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)
Related
I'm new to php and I feel that this language has some really good set of sorting functions for arrays
I have an input array below containing the books , readers and price
$input[]=array("Harrypotter","John",50);
$input[]=array("Twilight","John",60);
$input[]=array("Harrypotter","Jack",80);
$input[]=array("Gonegirl","marion",90);
$input[]=array("Gonegirl","test",90);
$input[]=array("Gonegirl","John",90);
$input[]=array("Gonegirl","eliza",90);
I want to sort the array on first field in such a way that the book which repeats more number of times should come first , the expected O/p is something like this
$input[]=array("Gonegirl","marion",90);
$input[]=array("Gonegirl","test",90);
$input[]=array("Gonegirl","John",90);
$input[]=array("Gonegirl","eliza",90);
$input[]=array("Harrypotter","Jack",80);
$input[]=array("Harrypotter","John",50);
$input[]=array("Twilight","John",60);
I'm able to copy the first field in separate array and get the occurrence by using array_count_values and i'm able to get the expected output through array_multi_sort but it sorts only by alphabetical order of the first field .
Any efficient way of arriving at the solution would be helpful !
You will first have to establish a count of how often each value occurs, then you can write a comparison function that sorts by those values:
$counts = array_count_values(array_map('current', $input));
usort($input, function (array $a, array $b) use ($counts) {
return $counts[$b[0]] - $counts[$a[0]];
});
This should work for you:
<?php
$input = array();
$input[]=array("Harrypotter","John",50);
$input[]=array("Twilight","John",60);
$input[]=array("Harrypotter","Jack",80);
$input[]=array("Gonegirl","marion",90);
$input[]=array("Gonegirl","test",90);
$input[]=array("Gonegirl","John",90);
$input[]=array("Gonegirl","eliza",90);
function cmp($a, $b) {
return strcmp($a[0], $b[0]);
}
usort($input, "cmp");
print_r($input);
?>
array_multisort(array_map('count', $input), SORT_DESC, $input);
PS: From PHP Sort a multidimensional array by number of items
Update:
array_multisort(array_map(function ($fld) {return $fld[0];}, $input), SORT_ASC, $input);
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);
Im stuck on a sorting problem, I have an array with 10 numbers (1-10) and I need to sort the in the following way where 10 would come after 1, for example...
desired outcome
$arr['a1','a10','a2','a3','a4','a5','a6','a7','a8','a9'];
actual outcome
$arr['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10'];
sort($arr);
$arr['a10','a1','a2','a3','a4','a5','a6','a7','a8','a9'];
I don't know the name of this type of sorting or how to perform it, if anyone could help me it would much appreciated.
NOTE: the numbers are part of a string
Try sort($arr,SORT_STRING) to explicitly treat the input as strings.
EDIT: Now that you've given your actual strings, try this:
usort($arr,function($a,$b) {
$a = explode("=",$a);
$b = explode("=",$b);
return $a[0] == $b[0] ? strcmp($a[1],$b[1]) : strcmp($a[0],$b[0]);
});
Sure, you want to sort alphabetically, not numerically.
sort($arr, SORT_STRING);
ref: http://php.net/manual/en/function.sort.php
You can change the behaviour of sort with it's second parameter.
Try this:
sort($arr, SORT_STRING);
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);
I can get a sorted filename list with the following code:
$log_files = scandir(LLP_LOG_DIR);
$sorted = sort($log_files);
the filename format is X.log, where X is a progressive numeric value.
How can I solve the problem of getting
0.log
1.log
10.log
11.log
2.log
3.log
where the wanted result is
0.log
1.log
2.log
3.log
[..]
9.log
10.log
11.log
[..]
I can strip the ".log" string, sort them etc, but what is the most efficient way?
try natsort instead,
natsort($log_files)
Set the second parameter of sort to SORT_NUMERIC to have numeric sorting:
$sorted = $log_files;
sort($sorted, SORT_NUMERIC);
And note that sort sorts the array of the variable given with the first parameter. The return value of sort is just a boolean value.
Try natcasesort instead.
natcasesort($array);
because natsort is case sensitive.