PHP: Sort an array by the length of its values?
PhpArraysPhp Problem Overview
I made an anagram machine and I have an array of positive matches. The trouble is they are all in a different order, I want to be able to sort the array so the longest array values appear first.
Anybody have any ideas on how to do this?
Php Solutions
Solution 1 - Php
Use http://us2.php.net/manual/en/function.usort.php
with this custom function
function sort($a,$b){
return strlen($b)-strlen($a);
}
usort($array,'sort');
Use uasort if you want to keep the old indexes, use usort if you don't care.
Also, I believe that my version is better because usort is an unstable sort.
$array = array("bbbbb", "dog", "cat", "aaa", "aaaa");
// mine
[0] => bbbbb
[1] => aaaa
[2] => aaa
[3] => cat
[4] => dog
// others
[0] => bbbbb
[1] => aaaa
[2] => dog
[3] => aaa
[4] => cat
Solution 2 - Php
If you'd like to do it the PHP 5.3 way, you might want to create something like this:
usort($array, function($a, $b) {
return strlen($b) - strlen($a);
});
This way you won't pollute your global namespace.
But do this only if you need it at a single place in your source code to keep things DRY.
Solution 3 - Php
PHP7 is coming. In PHP7, you could use the Spaceship Operator.
usort($array, function($a, $b) {
return strlen($b) <=> strlen($a);
});
Hope this could help you in the future.
Solution 4 - Php
function sortByLength($a,$b){
if($a == $b) return 0;
return (strlen($a) > strlen($b) ? -1 : 1);
}
usort($array,'sortByLength');
Solution 5 - Php
Descending Order:
$array = ['aa', 'bb', 'c', 'ccc', 'a', 'ertre'];
usort($array, function($a, $b){
return strlen($a) < strlen($b);
});
var_export($array);
// Output
array (
0 => 'ertre',
1 => 'ccc',
2 => 'aa',
3 => 'bb',
4 => 'c',
5 => 'a',
)
Ascending Order:
$array = ['aa', 'bb', 'c', 'ccc', 'a', 'ertre'];
usort($array, function($a, $b){
return strlen($a) > strlen($b);
});
// Output
array (
0 => 'c',
1 => 'a',
2 => 'aa',
3 => 'bb',
4 => 'ccc',
5 => 'ertre',
)
Solution 6 - Php
Make an array of strlen
of oyur array elements, and multisort
it with your array.
foreach($Yourarray as $c=>$key) {
$key['maxlen'] = strlen($key);
$sort_numcie[] = $key['maxlen'];
}
array_multisort($sort_numcie, $Yourarray);
This will definately work. I am sure!
Solution 7 - Php
In addition to the accepted answer, to sort an array by length with ascending OR descending order:
function strlen_compare($a,$b){
if(function_exists('mb_strlen')){
return mb_strlen($b) - mb_strlen($a);
}
else{
return strlen($b) - strlen($a);
}
}
function strlen_array_sort($array,$order='dsc'){
usort($array,'strlen_compare');
if($order=='asc'){
$array=array_reverse($array);
}
return $array;
}
Solution 8 - Php
array_multisort(array_map('count', $arr), SORT_DESC, $arr);
Solution 9 - Php
Here's a way using the spaceship operator (needs PHP 7.0):
$arr = ['apple','pear','oranges','banana'];
usort($arr, function ($a, $b) { return (strlen($a) <=> strlen($b)); });
print_r($arr);
Solution 10 - Php
Here is a snippet that demonstrates exactly why you should NOT use function calls inside of usort()
-- you will be needlessly calling strlen()
on values that were previously encountered.
In my 9-element array, array_multisort()
calls strlen()
9 times, but usort()
calls strlen()
23 times -- that's no bueno.
Code: (Demo)
function multisort($array) {
array_multisort(array_map('strlen', $array), SORT_DESC, $array);
printf(
"Total strlen() calls: %d\n%s\n---\n",
count($array),
var_export($array, true)
);
}
function usersort($array) {
usort(
$array,
function($a, $b) {
echo "strlen() called twice\n";
return strlen($b) <=> strlen($a);
}
);
var_export($array);
echo "\n---\n";
}
multisort($array);
usersort($array);
Output (from my own sample array):
Total strlen() calls: 9
array (
0 => 'eight',
1 => 'seven',
2 => 'three',
3 => 'five',
4 => 'four',
5 => 'nine',
6 => 'one',
7 => 'six',
8 => 'two',
)
---
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
strlen() called twice
array (
0 => 'three',
1 => 'seven',
2 => 'eight',
3 => 'four',
4 => 'five',
5 => 'nine',
6 => 'one',
7 => 'two',
8 => 'six',
)
---
Solution 11 - Php
Here's a way I've done it in the past.
// Here's the sorting...
$array = array_combine($words, array_map('strlen', $words));
arsort($array);
Solution 12 - Php
It's simple.
function LSort(a,b){return a.length-b.length;}
var YourArray=[[1,2,3,4,5,6], ['a','b'], ['X','Y','Z'], ['I','Love','You'], ['good man']];
YourArray.sort(Lsort);
Result:
['good man'] Length=1
['a','b'] Length=3
['X','Y','Z'] Length=3
['I','Love','You'] Length=3
[1,2,3,4,5,6] Length=6