Sam Becker
Sam Becker

Reputation: 19636

PHP: Sort an array by the length of its values?

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?

Upvotes: 70

Views: 77905

Answers (12)

Unknown
Unknown

Reputation: 46781

Use https://www.php.net/manual/en/function.usort.php

with this custom function

function sortByLength($a,$b){
    return strlen($b)-strlen($a);
}

usort($array,'sortByLength');

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

Upvotes: 165

mickmackusa
mickmackusa

Reputation: 47904

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',
)
---

Upvotes: 1

Swadhin Saha
Swadhin Saha

Reputation: 51

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',
)

Upvotes: 5

will
will

Reputation: 149

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);

Upvotes: 0

hgp
hgp

Reputation: 11

array_multisort(array_map('count', $arr), SORT_DESC, $arr);

Upvotes: 1

ZhenhangTung
ZhenhangTung

Reputation: 551

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.

Upvotes: 47

RafaSashi
RafaSashi

Reputation: 17205

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;
}

Upvotes: 0

Moinkhan Pathan
Moinkhan Pathan

Reputation: 9

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!

Upvotes: 0

xlttj
xlttj

Reputation: 1158

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.

Upvotes: 65

James Heo
James Heo

Reputation: 1

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

Upvotes: -8

joebeeson
joebeeson

Reputation: 4366

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);

Upvotes: -1

zalew
zalew

Reputation: 10311

function sortByLength($a,$b){
  if($a == $b) return 0;
  return (strlen($a) > strlen($b) ? -1 : 1);
}
usort($array,'sortByLength');

Upvotes: 10

Related Questions