Sort an array of objects in Java
This post will discuss how to sort an array of objects using the Comparable and Comparator interface in Java.
1. Using Comparable
To sort an array of objects using the Arrays.sort method in Java, we can have the class implement the Comparable interface, which then imposes a natural ordering on its objects.
If an object implements the Comparable interface, it needs to override its abstract method compareTo(), which compares an object with the specified object. The value returned by the compareTo() method decides the position of the object relative to the specified object.
If compareTo() returns a
- negative value, the object is less than the specified object.
- zero, the object is equal to the specified object.
- positive value, the object is greater than the specified object.
Here’s how we sort the array of the Student object using the Comparable interface, where the array is first ordered by age and then by name.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.*; class Student implements Comparable<Student> { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Student o) { if (this.age != o.getAge()) { return this.age - o.getAge(); } return this.name.compareTo(o.getName()); } } class Main { public static void main(String[] args) { Student[] students = { new Student("John", 15), new Student("Sam", 20), new Student("Dan", 20), new Student("Joe", 10) }; Arrays.sort(students); System.out.println(Arrays.toString(students)); } } |
Output:
[{name='Joe', age=10}, {name='John', age=15}, {name='Dan', age=20}, {name='Sam', age=20}]
2. Using Comparator
A Comparator is a comparison function that imposes a total ordering on some collection of objects and allows precise control over the sort order when passed to the Arrays.sort method.
If an object implements the Comparator interface, it needs to override the abstract method compare(), which compares its two arguments for order. The value returned by the compare() method decides the position of the first object relative to the second object.
If compare() returns a
- negative value, the first argument is less than the second.
- zero, the first argument is equal to the second.
- positive value, the first argument is greater than the second.
In the following example, we obtain a Comparator that compares Student objects first by their age and then by name.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.util.*; class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public int getAge() { return age; } } class Main { public static void main(String[] args) { Student[] students = { new Student("John", 15), new Student("Sam", 20), new Student("Dan", 20), new Student("Joe", 10) }; Arrays.sort(students, new Comparator<Student>() { @Override public int compare(Student first, Student second) { if (first.getAge() != second.getAge()) { return first.getAge() - second.getAge(); } return first.getName().compareTo(second.getName()); } }); System.out.println(Arrays.toString(students)); } } |
Output:
[{name='Joe', age=10}, {name='John', age=15}, {name='Dan', age=20}, {name='Sam', age=20}]
The above code can be easily shortened using lambda expressions:
|
1 2 3 4 5 6 7 |
Arrays.sort(students, (first, second) -> { if (first.getAge() != second.getAge()) { return first.getAge() - second.getAge(); } return first.getName().compareTo(second.getName()); }); |
Or even shorter:
|
1 2 3 |
Arrays.sort(students, (first, second) -> (first.getAge() != second.getAge()) ? (first.getAge() - second.getAge()) : (first.getName().compareTo(second.getName()))); |
We can also use the Comparator.thenComparing() method, which effectively combines two comparisons into one:
|
1 2 3 4 |
Comparator<Student> byAge = Comparator.comparing(Student::getAge); Comparator<Student> byName = Comparator.comparing(Student::getName); Arrays.sort(students, byAge.thenComparing(byName)); |
That’s all about sorting an array of objects in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)