Last active
November 4, 2019 12:24
-
-
Save juliendkim/71af9e59256ec8383c49064865a6129c to your computer and use it in GitHub Desktop.
Sort reverse by Unicode string length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $str1 | |
* @param string $str2 | |
* @return int | |
*/ | |
function sort_reverse_by_strlen($str1, $str2): int | |
{ | |
return (mb_strlen($str1) === mb_strlen($str2)) | |
? strnatcmp($str2, $str1) | |
: (mb_strlen($str2) - mb_strlen($str1)); | |
} | |
$array = array( | |
'ab cd', | |
'aaa', | |
'bcde', | |
'bbb' | |
); | |
usort($array, 'sort_reverse_by_strlen'); | |
print_r($array); | |
?> | |
RESULT | |
##################################### | |
Array | |
( | |
[0] => ab cd | |
[1] => bcde | |
[2] => bbb | |
[3] => aaa | |
) | |
##################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment