---
title: "Sorting using Comparator in Java"  
description: "In this article I am telling to example of comparator in java"  
author: "Manoj Pandey"  
published: 2015-04-23  
updated: 2017-12-06  
canonical: https://www.mindstick.com/articles/1740/sorting-using-comparator-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Sorting using Comparator in Java

A [comparison](https://www.mindstick.com/articles/23182/comparison-maruti-suzuki-celerio-v-s-tata-tiago) [function](https://www.mindstick.com/articles/43970/purchase-suitable-wedding-dresses-for-your-wedding-function), which imposes a total ordering on some [collection](https://www.mindstick.com/articles/1718/collections-in-java) of objects. Comparators can be passed to a sort method (such as [Collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp). Sort or Arrays. Sort) to allow precise [control](https://yourviews.mindstick.com/view/83439/bipartisan-gun-control-bill-passed-in-us-after-decades) over the sort order.\

Comparator is also used for [sorting](https://www.mindstick.com/articles/1581/sorting-list-with-side-bar). We can sort list, arrays, Collections using comparator in java. Comparator [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) 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](https://www.mindstick.com/forum/12904/main-method-returns-if-submethod-has-stoped-by-return-in-c-sharp) 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](https://www.mindstick.com/forum/157178/how-to-filter-1000-of-student-name-in-php-in-ascending-order).

```
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 nameclass 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 Idclass 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

---

Original Source: https://www.mindstick.com/articles/1740/sorting-using-comparator-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
