PHP: Sort an Array by the Length of Its Values

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

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

PHP: Sort array by value length

This should do it:

array_multisort(array_map('strlen', $array), $array);
  • Get the length of each string in an array by mapping strlen() using array_map()
  • Sort on the string lengths and sort the original array by the string length sorted array using array_multisort()

How to sort array of string by length and maintain their keys in PHP?

Use uasort:

uasort — Sort an array with a user-defined comparison function and maintain index association

usort doesn't maintain index associations.

Use it like this:

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

$arr = ['longstring', 'string', 'thelongeststring'];

uasort($arr, 'sortByLength');

print_r($arr);

eval.in demo

This returns:

Array
(
[2] => thelongeststring
[0] => longstring
[1] => string
)

sort array by length and then alphabetically

You can put both of the conditions into a usort comparison function.

usort($array, function($a, $b) {
return strlen($a) - strlen($b) ?: strcmp($a, $b);
});

The general strategy for sorting by multiple conditions is to write comparison expressions for each of the conditions that returns the appropriate return type of the comparison function (an integer, positive, negative, or zero depending on the result of the comparison), and evaluate them in order of your desired sort order, e.g. first length, then alphabetical.

If an expression evaluates to zero, then the two items are equal in terms of that comparison, and the next expression should be evaluated. If not, then the value of that expression can be returned as the value of the comparison function.


The other answer here appears to be implying that this comparison function does not return an integer greater than, less than, or equal to zero. It does.

How do I sort an array by string length then by value in PHP?

Incorporate both checks into your comparator:

function lensort($a,$b){
$la = strlen( $a); $lb = strlen( $b);
if( $la == $lb) {
return strcmp( $a, $b);
}
return $la - $lb;
}

You can see from this demo that this prints:

Array
(
[0] => A
[1] => B
[2] => C
[3] => AA
[4] => BB
[5] => BC
)

Sorting array based on array length

you should use usort.

function cmp($a, $b){
return (count($b) - count($a));
}
usort($array, 'cmp'); //$array is your array
$arrayAfterSort = $array;

Sorting an array of arrays by the child array's length?

This will work:

function sort_by_length($arrays) {
$lengths = array_map('count', $arrays);
asort($lengths);
$return = array();
foreach(array_keys($lengths) as $k)
$return[$k] = $arrays[$k];
return $return;
}

Note that this function will preserve the numerical keys. If you want to reset the keys, wrap it in a call to array_values().

PHP order array by length and then by index

Please use this method for sort array.

<?php
$parts = "I a o delicious cake";
$parts = explode(" ",$parts);
array_multisort(array_map('strlen', $parts),SORT_DESC, $parts); // this is for DESC order
//array_multisort(array_map('strlen', $parts),SORT_ASC, $parts); // you can use this for ASC order
echo "<pre>";
print_r($parts); //return like this "delicious cake I a o"
?>


Related Topics



Leave a reply



Submit