To sort the objects of user-defined classes, a comparator interface is utilized. A comparator object can compare two items of the same type.
This interface may be found in java.util package and includes two methods:
It supports different sorting sequences, which means you can sort the items based on any data member, such as rollno, name, age, or anything else.
class MyComparator implements Comparator<Class-name>{}
public int compare(Object obj1, Object obj2)
The following are the rules for utilizing the Comparator interface:
class Student {
// Attributes of a student
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name, String address)
{
// This keyword refers to current instance itself
this.rollno = rollno;
this.name = name;
this.address = address;
}
public String toString()
{
// Returning attributes of Student
return this.rollno + " " + this.name + " "
+ this.address;
}
}
Class to sort the entries according to the specified roll numbers.
class Sortbyroll implements Comparator<Student> {
// Method
// Sorting in ascending order of roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
class Coderz {
// Main driver method
public static void main(String[] args)
{
// Creating an empty ArrayList of Student type
ArrayList<Student> ar = new ArrayList<Student>();
// Adding entries in above List
// using add() method
ar.add(new Student(111, "Rabecca", "London"));
ar.add(new Student(102, "Tom", "Seoul"));
ar.add(new Student(123, "Tiffany", "Jaipur"));
ar.add(new Student(99, "John", "Hongkong"));
// Display message on console for better readability
System.out.println("Unsorted");
// Iterating over entries to print them
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
// Sorting student entries by roll number
Collections.sort(ar, new Sortbyroll());
// Display message on console for better readability
System.out.println("\nSorted by rollno");
// Again iterating over entries to print them
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
}
}
Unsorted
111 Rabecca London
102 Tom Seoul
123 Tiffany Jaipur
99 John Hongkong
Sorted by rollno
99 John Hongkong
102 Tom Seoul
111 Rabecca London
123 Tiffany Jaipur
Note: also read about the Comparable Interface in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…