php sort array by object value

Solutions on MaxInterview for php sort array by object value by the best coders in the world

showing results for - "php sort array by object value"
Ignacio
13 Sep 2017
1/**
2 * A generic PHP sorting algorithm that uses `usort` and `strcmp`.
3 * `usort` — Sort an array by values using a user-defined comparison function.
4 * `strcmp` — Returns < 0 if param 1 is less than param 2; > 0 if param 1 is greater than param 2, and 0 if they are equal.
5 */
6$questions = [
7  { id: 1, ordinal: 55 },
8  { id: 2, ordinal: 67 },
9  { id: 3, ordinal: 32 },
10];
11
12function sortByOrdinal($param1, $param2) {
13    return strcmp($param1->ordinal, $param2->ordinal);
14}
15
16/* `usort` alters an existing array. */
17usort($questions, "sortByOrdinal");
18
19/**
20 * $questions = [
21 *   { id: 3, ordinal: 32 },
22 *   { id: 1, ordinal: 55 },
23 *   { id: 2, ordinal: 67 },
24 * ];
25 */
Jana
10 Jan 2020
1<?php
2class Person {
3    var $first_name;
4    var $last_name;
5
6    function __construct( $fn, $ln ) {
7        $this->first_name = $fn;
8        $this->last_name = $ln;
9    }
10
11    public function get_first_name() {
12        return $this->first_name;
13    }
14
15    public function get_last_name() {
16        return $this->last_name;
17    }
18}
19
20$rob = new Person( 'Rob', 'Casabona' );
21$joe = new Person( 'Joe', 'Casabona' );
22$erin = new Person( 'Erin', 'Casabona' );
23$steve = new Person( 'Steve', 'Wozniack' );
24$bill = new Person( 'Bill', 'Gates' );
25$walt = new Person( 'Walt', 'Disney' );
26$bob = new Person( 'Bob', 'Iger' );
27
28$people = array( $rob, $joe, $erin, $steve, $bill, $walt, $bob );
29
30//The sorting
31usort($people, function($a, $b){
32    return [$a->get_last_name(), $a->get_first_name()] <=> [$b->get_last_name(), $b->get_first_name()];   
33});
34
35?>
36
37<pre>
38    <?php print_r($people);?>
39</pre>
queries leading to this page
php sort object by valuephp array of objects sort by propertyphp array of objects sortobect sort in phpphp order array by inner object valuesort array of arrays in php based on propertyphp sort object by optionssort objects by key phporder array object phpphp sort array object by valuesort object array by key phparray sort by object property phpphp usort custom object php sort object in array in ascending ordering by propphp object sort by valuephp sort array by opbject keyobject sort php by keysort array of object in phpphp array of objects order byphp object array order by valuephp order array of objectshowto sort array object particular value in phpsort by quantity of objects in list phpphp sort associative array of objectsphp array with object sortingphp order objectssort an object phpphp object array sort by key valuesort array of object by variable phpsort array of objects phpphp sort array of objectsphp object array sort by valuesorting php array objectssorting objects in phpapply sorting on object in phpphp order array by object propertysorting in object in phplaravel array of objects order ascphp orderby in a array of obsorting object arra phpphp sort array of objects by valuephp sort object arraysort array of objects by property phphow to sort array of object in php 3fphp sorting objectspass object in sort phpusort on object propertysort by object in phpsort object to array phpclass to sort an array phpphp object sort by keyhow to sort array of objects phphow to sort objects in phpobject to array sorting phpobject sort phpphp sort array by value in array of objectshow to sort object array in phphow to sort array of objects by value in phpphp sort array of objects by fieldarray object porperty sort phparray of object sort by a number phpsort object phpsort by object key value in phpsort object array in phpsort array of objects ascending order phpphp sort array by element propertyobject array sort by prop phpsortby php makes object valuephp asort array by objectsort array object phpphp sort object array desc numberphp sort array based on propertysort array of object php array object sort value phporder array object php by valuesort obj phphow to sort array of object in phpphp orderby in a array of objectssort objects phpphp sort by object valuehow to sort array of object by property in phpsort an array of objects by propert phpphp array object sortphp custom sort objectssort list by attribute phpsort an object array on key phpsort php array by a field in the object in phpphp sort object propertiesphp sort objectphp sort array of objects by namesort object array phparray of object sort by phparray sort objects phpsort array object php by valuephp sort by object valuesarray of objects sort by value in phporder array of objects by phpsort array of objects by object property phpphp sort object datasort objects in array condiition phpphp sort object array ascsort on array objects phpphp sort object by valuephp array object sort by valuephp sort object values optionsphp array sort by propertyphp custom sort array of objectsphp array with objects sortsort object array php by all elementsphp sort object values specificphp sort array of objects based on object propertyphp sort object values sort object by property value phpphp sort array of objects by idphp sort array of objects by property valuesort data php by object valuesort array of object by property phpphp sort array of objects by specific keyphp sort objecctorder list of objects by variable phpphp sort array by custom keysort an array of objects based on a key phpphp sort array by object propertyorder array of objects by property phpphp array sort objectsphp sort an object by attribute valuesort object in phpphp sort array of objects by numberphp sort objects arrayphp sort object values 22specific 22sort php array of objectsphp usort object arrayphp order array by property valuesorting in php with array of objectssot array of object by key phpsort php object by valuesort array by column stdclass object phpphp 8 sort object arrayclass to order an array phpphp sort object by fieldarray object field sortphp sort list of objectssort array of objects by object fields in phparray object field sort in phparray of object sort php by keyhow to sort array of objects in phpphp sort objects in arraysort array objects phpphp sort array by object valuesort array by object property phpsort objects in array phpsort objects in phpsort array of objects by object value phpphp order array of objects by valuephp array sort by object valuehow to sort array of array by property in phpphp sort array of objects by keysort array based on object value phpsort based on obj variable phpsort array of objects php based on property valuesort array values with keys phpphp sort array object by elementhow to sort array object by value in phpphp array sort objctssort ascending array of objects phpphp usort array of objectsphp sort objects array by propertysort php array of object by a field in phpphp sort array of objects by propertysort by array of object phpphp sort array by propertysort object php by valuephp sort array by object value