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 ($a->a < $b->a) ? -1 : 1; }); return $arr; } function sort_by_b($arr) { usort($arr, function($a, $b) { if ($a->b == $b->b) { return 0; } return ($a->b < $b->b) ? -1 : 1; }); return $arr; } function sort_by_c($arr) { usort($arr, function($a, $b) { if ($a->c == $b->c) { return 0; } return ($a->c < $b->c) ? -1 : 1; }); return $arr; } $arr = sort_by_c($arr); $arr = sort_by_b($arr); $arr = sort_by_a($arr); var_dump($arr);
based on E94ha
Output for 7.0.0 - 7.2.0
array(18) { [0]=> object(A)#18 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "f" ["c"]=> string(1) "L" } [1]=> object(A)#12 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "m" ["c"]=> string(1) "M" } [2]=> object(A)#8 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "f" ["c"]=> string(1) "S" } [3]=> object(A)#5 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "m" ["c"]=> string(1) "S" } [4]=> object(A)#4 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "m" ["c"]=> string(1) "L" } [5]=> object(A)#14 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "f" ["c"]=> string(1) "M" } [6]=> object(A)#2 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "m" ["c"]=> string(1) "L" } [7]=> object(A)#13 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "m" ["c"]=> string(1) "M" } [8]=> object(A)#15 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "f" ["c"]=> string(1) "L" } [9]=> object(A)#1 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "f" ["c"]=> string(1) "M" } [10]=> object(A)#11 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "f" ["c"]=> string(1) "S" } [11]=> object(A)#3 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "m" ["c"]=> string(1) "S" } [12]=> object(A)#17 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "f" ["c"]=> string(1) "L" } [13]=> object(A)#10 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "f" ["c"]=> string(1) "M" } [14]=> object(A)#7 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "m" ["c"]=> string(1) "M" } [15]=> object(A)#16 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "m" ["c"]=> string(1) "L" } [16]=> object(A)#6 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "m" ["c"]=> string(1) "S" } [17]=> object(A)#9 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "f" ["c"]=> string(1) "S" } }
Output for 5.3.19 - 5.6.28
array(18) { [0]=> object(A)#5 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "m" ["c"]=> string(1) "S" } [1]=> object(A)#4 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "m" ["c"]=> string(1) "L" } [2]=> object(A)#12 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "m" ["c"]=> string(1) "M" } [3]=> object(A)#14 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "f" ["c"]=> string(1) "M" } [4]=> object(A)#18 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "f" ["c"]=> string(1) "L" } [5]=> object(A)#8 (3) { ["a"]=> string(6) "a1234d" ["b"]=> string(1) "f" ["c"]=> string(1) "S" } [6]=> object(A)#3 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "m" ["c"]=> string(1) "S" } [7]=> object(A)#13 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "m" ["c"]=> string(1) "M" } [8]=> object(A)#11 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "f" ["c"]=> string(1) "S" } [9]=> object(A)#2 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "m" ["c"]=> string(1) "L" } [10]=> object(A)#15 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "f" ["c"]=> string(1) "L" } [11]=> object(A)#1 (3) { ["a"]=> string(6) "a1235d" ["b"]=> string(1) "f" ["c"]=> string(1) "M" } [12]=> object(A)#17 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "f" ["c"]=> string(1) "L" } [13]=> object(A)#7 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "m" ["c"]=> string(1) "M" } [14]=> object(A)#9 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "f" ["c"]=> string(1) "S" } [15]=> object(A)#10 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "f" ["c"]=> string(1) "M" } [16]=> object(A)#6 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "m" ["c"]=> string(1) "S" } [17]=> object(A)#16 (3) { ["a"]=> string(6) "a1236d" ["b"]=> string(1) "m" ["c"]=> string(1) "L" } }