Skip to content

Instantly share code, notes, and snippets.

@david-torres
Last active June 15, 2017 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-torres/f6c17dc239d2b2581b1b to your computer and use it in GitHub Desktop.
Save david-torres/f6c17dc239d2b2581b1b to your computer and use it in GitHub Desktop.
[sort hex] Sort an array of hexadecimal numbers in PHP #php
<?php
$hexValues = array(
dechex(10), // a
dechex(42), // 2a
dechex(111), // 6f
dechex(78), // 4e
dechex(16) // 10
);
usort($hexValues, function($a, $b){
$a_dec = hexdec($a);
$b_dec = hexdec($b);
if ($a_dec == $b_dec){
return 0;
} elseif ($a_dec > $b_dec){
return -1;
} else {
return 1;
}
});
var_dump($hexValues);
/* output
array(5) {
[0] =>
string(2) "6f"
[1] =>
string(2) "4e"
[2] =>
string(2) "2a"
[3] =>
string(2) "10"
[4] =>
string(1) "a"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment