Home » Java programming language

How to sort objects of the Collection in Java?

Sorting objects of the Collection: Here, we are going to learn how to sort the objects of the Collection in Ascending and Descending in Java programming language?
Submitted by Preeti Jain, on August 04, 2019

Sorting objects of the Collection

  • This concept is related to sorting and here we will see how to sort objects on the Collection?
  • In java, we have utility class Collections which provide various methods to perform various task and one of the methods of Collection class is related to sorting like sort().
  • We can implement sorting on Collection object in two ways:
    1. By using Comparable
    2. By using Comparator
  • When we call Collections.sort(). It sorts an object based on natural sorting or default sorting(i.e Ascending order) that's is specified in compareTo() method.
  • When we call Collections.sort(Comparator). It sorts an object based on customized sorting (i.e Ascending order or Descending order) that's is specified in compare() method of Comparator.

We will see the sorting ways one by one...

1) By using Comparator

  • If we pass the Comparator object in Collection class constructor then our compare() method will be executed.
  • When we want customize sorting then we should go for Comparator.
  • It is possible to implement customized sorting by using Comparator interface. (Customized sorting means that according to our need whether it is ascending or descending).

Example:

import java.util.*;

class TreeSetClass {
    public static void main(String[] args) {
        // Here we are passing Comparator object in Collection 
        // class constructor for custoize sorting
        TreeSet ts = new TreeSet(new CustomizeSorting());

        // adding elements to TreeSet
        ts.add(10);
        ts.add(40);
        ts.add(30);
        ts.add(20);

        // Customized Sorted List
        System.out.println("Customize sorting :" + ts);
    }
}

// Here we are implementing Comparator interface
class CustomizeSorting implements Comparator {
    // Here we are overrding compare() method of Comparator
    public int compare(Object obj1, Object obj2) {

        Integer i1 = (Integer) obj1;
        Integer i2 = (Integer) obj2;

        return -i1.compareTo(i2);
    }
}

Output

E:\Programs>javac TreeSetClass.java

E:\Programs>java TreeSetClass
Customize sorting :[40, 30, 20, 10]

2) By using Comparable interface

  • For predefined Comparable classes default natural sorting is already available.
  • For predefined Non-Comparable classes default natural sorting is not already available.
  • For our customized classes to define natural sorting then we should go for Comparable.
  • In case of default natural sorting compulsory object should be homogenous and Comparable otherwise we will get CCE (ClassCastException).

Example:

import java.util.*;

class TreeSetClass {
    public static void main(String[] args) {
        Student s1 = new Student(10);
        Student s2 = new Student(30);
        Student s3 = new Student(70);
        Student s4 = new Student(20);

        // Here we are not passing Comparator object in Collection 
        // class constructor for default sorting
        TreeSet ts = new TreeSet();

        // adding elements to TreeSet
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);

        // Customized Sorted List
        System.out.println("Default sorting :" + ts);
    }
}

// Here we are implementing Comparable interface
class Student implements Comparable {
    int code;

    Student(int code) {
        this.code = code;
    }

    public String toString() {
        return " Code - " + code;
    }

    // Here we are overrding compare() method of Comparable interface
    public int compareTo(Object obj) {
        int code1 = this.code;
        Student intermediate = (Student) obj;
        int code2 = intermediate.code;

        if (code1 < code2)
            return -1;
        else if (code1 > code2)
            return +1;
        else
            return 0;
    }
}

Output

E:\Programs>javac TreeSetClass.java

E:\Programs>java TreeSetClass
Default sorting :[ Code - 10,  Code - 20,  Code - 30,  Code - 70]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.