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.
- How to sort an Array in C# | Array.Sort() Method Set – 2
- Intermediate fields in Django | Python
- Array.BinarySearch(Array, Object) Method with examples in C#
- Python | Relational fields in Django models
- How to select text input fields using CSS selector ?
- Mathematics | Rings, Integral domains and Fields
- Convert an object to associative array in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.