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?
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?
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
function sortByLength($a,$b){
if($a == $b) return 0;
return (strlen($a) > strlen($b) ? -1 : 1);
}
usort($array,'sortByLength');
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',
)
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!
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);
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',
)
---