Home > DeveloperSection > Articles > Sorting using Comparator in Java

Sorting using Comparator in Java


Java Java 
Ratings:
0 Comment(s)
 1153  View(s)
Rate this:

Comparator example in Java

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections. Sort or Arrays. Sort) to allow precise control over the sort order.

Comparator is also used for sorting. We can sort list, arrays, Collections using comparator in java. Comparator interface is used to order the objects of user-defined class.

The compare Method:

int compare(Object obj1, Object obj2)

obj1 and obj2 are the objects to be compared. This method returns zero if the objects are equal. It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned.

The equals Method:

The equals( ) method, shown here, tests whether an object equals the invoking comparator:

boolean equals(Object obj)

Here I am creating a sample for sorting order by student id and student name.

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;

 

class Sample {

 

     @SuppressWarnings("unchecked")

     public static void main(String args[]) {

          // create object of Mathematical class

          ArrayList<Student> myList = new ArrayList<Student>();

 

          myList.add(new Student(101, "Zack", 22));

          myList.add(new Student(104, "Anderson", 26));

          myList.add(new Student(103, "Joolie", 24));

          myList.add(new Student(106, "Raakel", 28));

          myList.add(new Student(105, "Abbas", 23));

          myList.add(new Student(102, "Ukrit", 21));

          myList.add(new Student(107, "Abbe", 29));

          Collections.sort(myList, new NameComprator());

          Iterator<Student> iter = myList.iterator();

          System.out.println("Sorting by Name\n");

          while (iter.hasNext()) {

              Student student = (Student) iter.next();

       System.out.println(student.sId + "     " + student.Sname);

          }

 

          Collections.sort(myList, new IdComprator());

          Iterator<Student> iter1 = myList.iterator();

          System.out.println("\nSorting by Id\n");

          while (iter1.hasNext()) {

              Student student = (Student) iter1.next();

      System.out.println(student.sId + "     " + student.Sname);

          }

     }

 

}

 

class Student {

     int sId;

     String Sname;

     int age;

 

     public Student(int id, String name, int age) {

          this.sId = id;

          this.Sname = name;

          this.age = age;

 

     }

 

}

 

// sorted by name

class NameComprator implements Comparator<Object> {

 

     @Override

     public int compare(Object obj1, Object obj2) {

          // TODO Auto-generated method stub

          Student s1 = (Student) obj1;

          Student s2 = (Student) obj2;

          return s1.Sname.compareTo(s2.Sname);

     }

}

// Sorted by Id

class IdComprator implements Comparator<Object> {

 

     @Override

     public int compare(Object obj1, Object obj2) {

          // TODO Auto-generated method stub

          Student s1 = (Student) obj1;

          Student s2 = (Student) obj2;

          if (s1.sId == s2.sId)

              return 0;

          else if (s1.sId > s2.sId)

              return 1;

          else

              return -1;

     }

}

 

Output -:

Sorting by Name 

105     Abbas

107     Abbe

104     Anderson

103     Joolie

106     Raakel

102     Ukrit

101     Zack

 

Sorting by Id

 

101     Zack

102     Ukrit

103     Joolie

104     Anderson

105     Abbas

106     Raakel

107    Abbe

 

 

               

 


Don't want to miss updates? Please click the below button!

Follow MindStick