function myFunction($a, $b)
{
if ($a > $b) return 1;
if ($a == $b) return 0;
return -1;
}
$array = [2, 4, 1, 3];
usort($array, 'myFunction');
print_r($array);
// output
// Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// [3] => 4
// )
Visual representation on how it works
Comments
Post a Comment