Friday, October 17, 2025
HomeLanguagesSort array of objects by object fields in PHP

Sort array of objects by object fields in PHP

Given an array of objects and the task is to sort the array by object by given fields.

Approach:
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.

Example:




<?php
  
// PHP program to show sorting of 
// array of objects by object fields
  
// School Data array
$gfg_array = array(
    array(
        'score' => '100',
        'name' => 'Sam',
        'subject' => 'Data Structures'
    ),
    array(
        'score' => '50',
        'name' => 'Tanya',
        'subject' => 'Advanced Algorithms'
    ),
    array(
        'score' => '75',
        'name' => 'Jack',
        'subject' => 'Distributed Computing'
    )
);
  
// Class for encapsulating school data
class geekSchool {
      
    var $score, $name, $subject;
  
    // Constructor for class initialization
    public function geekSchool($data) {
        $this->name = $data['name'];
        $this->score = $data['score'];
        $this->subject = $data['subject'];
    }
}
  
// Function to convert array data to class object
function data2Object($data) {
    $class_object = new geekSchool($data);
    return $class_object;
}
  
// Comparator function used for comparator
// scores of two object/students
function comparator($object1, $object2) {
    return $object1->score > $object2->score;
}
  
// Generating array of objects
$school_data = array_map('data2Object', $gfg_array);
  
// Printing original object array data
print("Original object array:\n");
  
print_r($school_data);
  
// Sorting the class objects according 
// to their scores
usort($school_data, 'comparator');
  
// Printing sorted object array data
print("\nSorted object array:\n");
  
print_r($school_data);
  
?>


Output:

Original object array:
Array
(
    [0] => geekSchool Object
        (
            [score] => 100
            [name] => Sam
            [subject] => Data Structures
        )

    [1] => geekSchool Object
        (
            [score] => 50
            [name] => Tanya
            [subject] => Advanced Algorithms
        )

    [2] => geekSchool Object
        (
            [score] => 75
            [name] => Jack
            [subject] => Distributed Computing
        )

)

Sorted object array:
Array
(
    [0] => geekSchool Object
        (
            [score] => 50
            [name] => Tanya
            [subject] => Advanced Algorithms
        )

    [1] => geekSchool Object
        (
            [score] => 75
            [name] => Jack
            [subject] => Distributed Computing
        )

    [2] => geekSchool Object
        (
            [score] => 100
            [name] => Sam
            [subject] => Data Structures
        )

)
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS