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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…