PHP Array Sorting alphanumeric with array_multisort - php

I would like to use the functionality of SORT_ASC, SORT_DESC with array_multisort()
but the problem is, it sorts my array like
test.1
test.10
test.2
test.3
test.4
test.5
it should be
test.1
test.2
test.3
test.4
...
test.10
currently using it like this
array_multisort(($sortc), (($sortby==='asc') ? SORT_ASC : SORT_DESC), $pool);
$sortc is the array I want to sort from the multi-dimensional array of $pool
I do know that natsort can do it properly, but it doesnt have the same functionality as array_multisort.

You want to use natural sort. Just add the SORT_NATURAL flag.
array_multisort(($sortc), (($sortby==='asc') ? SORT_ASC : SORT_DESC), SORT_NATURAL, $pool);

Related

Using array_multisort to sort integer ascending and sort corresponding string accordingly (php)

Using array_multisort, how can I sort prices from lowest to highest and then use that sorting order to sort its corresponding title?
Arrays
$pricearray = array(4.00, 56.99, 3.19);
$headerarray = array('four', 'fifty-six', 'three');
Desired Output
$pricearray = array(3.19, 4.00, 56.99);
$headerarray = array('three', 'four', 'fifty-six');
My Attempt
array_multisort($headerarray, $pricearray, SORT_ASC);
Sort $pricearray ascending (the default) and array_multisort will sort $headerarray with it:
array_multisort($pricearray, $headerarray);
To specify the order use it as the argument after the array:
array_multisort($pricearray, SORT_ASC, $headerarray);
See the manual where it states that some arguments can be swapped or omitted:
array1_sort_order The order used to sort the previous array argument.
Either SORT_ASC to sort ascendingly or SORT_DESC to sort descendingly.
This argument can be swapped with array1_sort_flags or omitted
entirely, in which case SORT_ASC is assumed.
array1_sort_flags Sort options for the previous array argument:
This argument can be swapped with array1_sort_order or omitted
entirely, in which case SORT_REGULAR is assumed.

Using array_multisort to sort date of type '28-jul-17'?

I have data that is in a multi-dimensional array that has a date attached to it of the format '28-Jul-17'. I would like to sort my data in ascending or descending date order. I am able to sort my multi-dimensional array, but I do not know how to sort the data out. My question is, which flag should I use to in this function to get the result that I require? At the moment I am using SORT_DESC or SORT ASC and this does not sort my date out in chronological order.
I am using the following:
array_multisort($fieldOfInterest, SORT_DESC , $arrayOfDictionary);
Sorting type flags:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
SORT_NATURAL
You can't do it with default flags of array_multisort(), because they don't know about custom-date-formats.
Instead of it you can do something like below:-
function compare_dates($a, $b)
{
$t1 = strtotime($a['date']);
$t2 = strtotime($b['date']);
return ($t1 >$t2) ? 1:-1; //changing 1 and -1 position will make it descending
}
usort($array, 'compare_dates'); // here $array is multi-dimensional array
Output:- https://eval.in/842881 OR https://eval.in/842882

Sorting array of strings by contained numbers?

I have those elements in an array:
/mypath/10_something_04_06.txt
/mypath/1_something_04_06.txt
/mypath/12_something_04_06.txt
/mypath/2_something_04_06.txt
/mypath/3_something_04_06.txt
Now I want to sort it, so that the numbers after /mypath/ are considered. This is what I tried:
sort($myArray, SORT_NUMERIC);
sort($myArray, SORT_STRING );
It doesn't work.
Should be expected result like below:
/mypath/1_something_04_06.txt
/mypath/2_something_04_06.txt
/mypath/3_something_04_06.txt
/mypath/10_something_04_06.txt
/mypath/12_something_04_06.txt
Thanks!
You are looking for natsort (Sort an array using a "natural order" algorithm)

sort array by keys and reverse the result

I have a array like this:
$array = array(
[1]=>'something',
[0.2]=>'something',
[0.1]=>'something',
[0.3]=>'something',
[0.10]=>'something'
);
Now i like to sort this array by key, so for that i am using this code:
uksort($array, 'strnatcasecmp');
The above code works fine, but the only problem is that i want to reverse the result.
For this purpose i used krsort , array_reverse , rsort after uksort, but all of them change uksort's result.
So have can i sort this array by key in natural order and reverse the result?
What i want should be like :
$array = array(
[1]=>'something',
[0.10]=>'something',
[0.3]=>'something',
[0.2]=>'something',
[0.1]=>'something'
);
Try this:
uksort($array, create_function('$a,$b', 'return -strnatcasecmp($a,$b);'));
Since you already use a variant of uksort (user-function defined sort), this version just reverses the order by inverting the result of the comparison function. I think it should work for you.
Alternatively try this:
uksort($array, 'strnatcasecmp');
$array = array_reverse($array, true);
Note the true parameter, that preserves your keys.
Update for modern PHP versions, since create_function is deprecated:
uksort($array, function ($a, $b) { return -strnatcasecmp($a, $b); });
Update for PHP 7.4 with new syntax (not released as of writing):
uksort($array, fn($a, $b) => -strnatcasecmp($a, $b));
For PHP 4+, you can use krsort:
krsort($array, SORT_NATURAL | SORT_FLAG_CASE);
Looks simpler to me, and it works the same as strnatcasecmp(). If you don't want it to be case sensitive, get rid of the SORT_FLAG_CASE flag.
I'm not a php programmer, but according to
http://www.php.net/manual/en/function.uksort.php
you should be able to write your own call back function for the comparison and then sort them anyway you want to.
asort() will sort by the key, arsort() will sort by the key and reverse the order.

How to sort an array in PHP while it retrieve using forloop

Here is my actual requirement (edited)
array('1-One','2-second','10-ten','3-third','11-elevan','4-four')
How can I print the above array as
1-One
2-second
3-third
4-four
10-ten
11-elevan
ie by the sort order of prefix number
sort($array,SORT_NUMERIC);//add extra parameter to change default sorting behave
print_r($array);
DEMO
Use sort before you print your array.
$array = array(1,10,2,12,3);
sort($array);
print_r($array);

Categories