3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class A { public $a; public $b; public $c; public function __construct($a, $b, $c) { $this->a = $a; $this->b = $b; $this->c = $c; } } $arr = Array( new A('a1235d', 'f', 'M'), new A('a1235d', 'm', 'L'), new A('a1235d', 'm', 'S'), new A('a1234d', 'm', 'L'), new A('a1234d', 'm', 'S'), new A('a1236d', 'm', 'S'), new A('a1236d', 'm', 'M'), new A('a1234d', 'f', 'S'), new A('a1236d', 'f', 'S'), new A('a1236d', 'f', 'M'), new A('a1235d', 'f', 'S'), new A('a1234d', 'm', 'M'), new A('a1235d', 'm', 'M'), new A('a1234d', 'f', 'M'), new A('a1235d', 'f', 'L'), new A('a1236d', 'm', 'L'), new A('a1236d', 'f', 'L'), new A('a1234d', 'f', 'L') ); function sort_by_a($arr) { usort($arr, function($a, $b) { if ($a->a == $b->a) { return 0; } return strcmp($a->a, $b->a); }); return $arr; } function sort_by_b($arr) { usort($arr, function($a, $b) { if ($a->b == $b->b) { return 0; } return strcmp($a->b, $b->b); }); return $arr; } function sort_by_c($arr) { usort($arr, function($a, $b) { if ($a->c == $b->c) { return 0; } return strcmp($a->c, $b->c); }); return $arr; } function printarr($arr) { foreach ($arr as $a) echo $a->a."\t".$a->b."\t".$a->c."\n"; echo "\n"; } $arr = sort_by_c($arr); printarr($arr); $arr = sort_by_b($arr); printarr($arr); $arr = sort_by_a($arr); printarr($arr);
based on vvhqr
Output for 7.0.0 - 7.2.0
a1234d f L a1235d m L a1236d f L a1234d m L a1236d m L a1235d f L a1234d f M a1235d m M a1234d m M a1235d f M a1236d m M a1236d f M a1235d f S a1236d f S a1234d f S a1236d m S a1234d m S a1235d m S a1234d f L a1236d f M a1234d f S a1236d f S a1235d f S a1235d f M a1234d f M a1235d f L a1236d f L a1235d m M a1234d m M a1235d m L a1236d m M a1236d m L a1234d m L a1236d m S a1234d m S a1235d m S a1234d f L a1234d m M a1234d f S a1234d m S a1234d m L a1234d f M a1235d m L a1235d m M a1235d f L a1235d f M a1235d f S a1235d m S a1236d f L a1236d f M a1236d m M a1236d m L a1236d m S a1236d f S
Output for 5.3.19 - 5.6.28
a1236d f L a1235d f L a1234d m L a1236d m L a1234d f L a1235d m L a1234d f M a1234d m M a1236d f M a1235d f M a1235d m M a1236d m M a1236d f S a1235d m S a1234d m S a1236d m S a1234d f S a1235d f S a1235d f M a1236d f L a1236d f S a1234d f S a1235d f S a1234d f M a1236d f M a1235d f L a1234d f L a1234d m S a1236d m S a1234d m L a1235d m S a1236d m L a1235d m L a1235d m M a1236d m M a1234d m M a1234d m S a1234d m L a1234d m M a1234d f M a1234d f L a1234d f S a1235d m S a1235d m M a1235d f S a1235d m L a1235d f L a1235d f M a1236d f L a1236d m M a1236d f S a1236d f M a1236d m S a1236d m L