Open In App

How to Sort an Arrays of Object using Arrays.sort()

Last Updated : 13 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort().

Sorting an Array of Object

With Array of Object what it really means is an array storing any type of object can be Integer, String or User Defined. Let us check it with an example mentioned below:

Input : { { Bob ,25 } , { Charlie , 35 } , { Alice , 30} }

Output:
Sorting Objects according to age : { { Bob , 25 } , { Alice , 30} , { Charlie , 35 } }
Sorting Objects according to name : { { Alice , 30} , { Bob ,25 } , { Charlie , 35 } }

Programs for Sorting an Array of Objects Using Arrays.sort():

Below is the example using the custom Person class and sorting an array in both ascending order and descending order.

Java
import java.util.Arrays;
import java.util.Comparator;

// Person Class
class Person {
    String name;
    int age;

      // Constructor
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

      // Overriding toString Method for the Class
    @Override
    public String toString() {
        return name + " (" + age + ")";
    }
}

// Main Class
public class Main {
      // Main Method
    public static void main(String[] args) {
        Person[] people = {
            new Person("Alice", 30),
            new Person("Bob", 25),
            new Person("Charlie", 35)
        };

        // Sort by age in ascending order
        Arrays.sort(people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return Integer.compare(p1.age, p2.age);
            }
        });
      
        System.out.println("Sorting elements according to order by age: " + Arrays.toString(people));


        // Sort by name in ascending order
        Arrays.sort(people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return p1.name.compareTo(p2.name);
            }
        });
      
        System.out.println("Sorting elements according to order by name: " + Arrays.toString(people));
    }
}

Output:

Ascending order by age: [Bob (25), Alice (30), Charlie (35)]
Descending order by age: [Charlie (35), Alice (30), Bob (25)]
Ascending order by name: [Alice (30), Bob (25), Charlie (35)]
Descending order by name: [Charlie (35), Bob (25), Alice (30)]

Explanation of above program:

  • The Person class has two fields called as name and age.
  • It is override toString method to provide the string representation of Person object.
  • A Comparator is implemented the inline using an anonymous class.
  • Arrays.sort (people, new Comparator<Person>) () {...}) is sort the array based on the age in ascending order.
  • The Comparator is modified to the reverse order by swapping the p1 and p2 in compare method.
  • Another Comparator is used to sort an array based on name field in the ascending order.
  • The Comparator is modified to reverse order by swapping the p1 and p2 in compare method for the names.

Next Article
Article Tags :
Practice Tags :

Similar Reads