Last active
December 22, 2015 23:09
-
-
Save lelotnk/6544868 to your computer and use it in GitHub Desktop.
Ordenação de strings.
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 | |
// Criar uma lista de testes. | |
$array = array(); | |
$letter ='A'; | |
for ($i = 0; $i <= 1000; $i++) { | |
for ($j = 1; $j < 13; $j++) { | |
$array[] = $letter . $j; | |
} | |
$letter++; | |
} | |
shuffle($array); // Embaralhar para o teste. | |
// Ordenar | |
usort($array, function($a, $b) { | |
list($la, $na) = preg_split('#(\d+)$#', $a, 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); | |
list($lb, $nb) = preg_split('#(\d+)$#', $b, 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); | |
if (strlen($la) < strlen($lb)) { | |
return -1; | |
} elseif (strlen($la) > strlen($lb)) { | |
return 1; | |
} else { | |
$strcmp = strcmp($la, $lb); | |
$intcmp = ($na == $nb) ? 0 : (($na < $nb) ? -1 : 1); | |
return ($strcmp == 0) ? $intcmp : $strcmp; | |
} | |
}); | |
echo '<pre>'; | |
print_r($array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment