Given an array of objects and the task is to sort the array by object by given fields.
The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field. Call usort() function with first argument as the array of objects and second argument as the comparator function on the basis of which comparison between two array objects has to be made.
Original object array: Array (  => geekSchool Object ( [score] => 100 [name] => Sam [subject] => Data Structures )  => geekSchool Object ( [score] => 50 [name] => Tanya [subject] => Advanced Algorithms )  => geekSchool Object ( [score] => 75 [name] => Jack [subject] => Distributed Computing ) ) Sorted object array: Array (  => geekSchool Object ( [score] => 50 [name] => Tanya [subject] => Advanced Algorithms )  => geekSchool Object ( [score] => 75 [name] => Jack [subject] => Distributed Computing )  => geekSchool Object ( [score] => 100 [name] => Sam [subject] => Data Structures ) )
- Extract unique objects by attribute from array of objects.
- Mathematics | Rings, Integral domains and Fields
- How to select text input fields using CSS selector ?
- Intermediate fields in Django | Python
- Python | Relational fields in Django models
- Scala | Controlling visibility of constructor fields
- Difference between private keyword and private fields in TypeScript
- strings.Fields() Function in Golang With Examples
- How to add input fields dynamically on button click in AngularJS ?
- How to Access Interface Fields in Golang?
- How to compare Structs with the Different Values Assigned to Data Fields in Golang?
- How to detect the duplication of values of input fields ?
- PHP | Type Casting and Conversion of an Object to an Object of other class
- How to sort an Array in C# | Array.Sort() Method Set – 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.