Sorting Objects Using Collections Sort

How do we do sorting of objects using Collections.sort method?

A java programmer usually required to do coding to sort a given set of inputs. Java has inbuilt set of API’s for sorting. There is a Comparable interface in java that needs to be implemented by class whose object needs to be compared. Java classes like “Integer”, “Date” implements this interface, so object of these classes can be sorted easily by Collection’s sort method. We can make a class that implements Comparable interface so that it’s objects can be sorted by “Collections.sort” method.

Using Collections.sort to sort custom class and user defined Comparator

Code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;

class Employee implements Comparable {
  private String name;

  private double salary;

  Employee(String name, double salary) {
    this.name = name;
    this.salary = salary;
  }

  String getName() {
    return name;
  }

  double getSalary() {
    return salary;
  }

  public String toString() {
    return "Name = " + getName() + ", Salary = " + getSalary();
  }

  public int compareTo(Object o) {
    if (!(o instanceof Employee))
      throw new ClassCastException();

    Employee e = (Employee) o;

    return name.compareTo(e.getName());
  }

  static class SalaryComparator implements Comparator {
    public int compare(Object o1, Object o2) {
      if (!(o1 instanceof Employee) || !(o2 instanceof Employee))
        throw new ClassCastException();

      Employee e1 = (Employee) o1;
      Employee e2 = (Employee) o2;

      return (int) (e1.getSalary() - e2.getSalary());
    }
  }
}

class UtilDemo4 {
  public static void main(String[] args) {
    String[] names = { "A", "B", "C", "D" };

    double[] salaries = { 2.0, 5.0, 6.0, 4.0 };

    List l = new ArrayList();

    for (int i = 0; i < names.length; i++)
      l.add(new Employee(names[i], salaries[i]));

    Collections.sort(l);

    ListIterator liter = l.listIterator();

    while (liter.hasNext())
      System.out.println(liter.next());

    Collections.sort(l, new Employee.SalaryComparator());

    liter = l.listIterator();

    while (liter.hasNext())
      System.out.println(liter.next());
  }
}

Java Tips

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.