Last active
June 15, 2017 15:40
-
-
Save david-torres/f6c17dc239d2b2581b1b to your computer and use it in GitHub Desktop.
[sort hex] Sort an array of hexadecimal numbers in PHP #php
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 | |
$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