Comparable and Comparator
What is the Comparable interface ?
The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.
The Comparable interface in the generic form is written as follows:
interface Comparable<T>
where T is the name of the type parameter.
All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of the compareTo() method is as follows:
int i = object1.compareTo(object2)
The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.
The Comparable interface in the generic form is written as follows:
interface Comparable<T>
where T is the name of the type parameter.
All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of the compareTo() method is as follows:
int i = object1.compareTo(object2)
- If object1 < object2: The value of i returned will be negative.
- If object1 > object2: The value of i returned will be positive.
- If object1 = object2: The value of i returned will be zero.
EXAMPLE 1
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ComparableDemo {
public static void main(String[] args) {
List ts1 = new ArrayList();
ts1.add(new Employee("Thamil", 4000.00));
ts1.add(new Employee("Sathya", 2000.00));
ts1.add(new Employee("Kathir", 3000.00));
ts1.add(new Employee("Priya", 1000.00));
Collections.sort(ts1);
Iterator itr = ts1.iterator();
while (itr.hasNext()) {
Object element = itr.next();
System.out.println(element + "\n");
}
}
}
class Employee implements Comparable {
int EmpID;
String Ename;
double Sal;
static int i;
public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
}
public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;
}
public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal" + Sal;
}
public int compareTo(Object o1) {
if (this.Sal == ((Employee) o1).Sal)
return 0;
else if ((this.Sal) > ((Employee) o1).Sal)
return 1;
else
return -1;
}}
EXAMPLE 2
public class Employee implements Comparable {
int EmpID;
String Ename;
double Sal;
static int i;
public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
}
public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;
}
public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal" + Sal;
}
public int compareTo(Object o1) {
if (this.Sal == ((Employee) o1).Sal)
return 0;
else if ((this.Sal) > ((Employee) o1).Sal)
return 1;
else
return -1;
}
}
ComparableDemo.java
import java.util.*;
public class ComparableDemo{
public static void main(String[] args) {
List ts1 = new ArrayList();
ts1.add(new Employee ("Tom",40000.00));
ts1.add(new Employee ("Harry",20000.00));
ts1.add(new Employee ("Maggie",50000.00));
ts1.add(new Employee ("Chris",70000.00));
Collections.sort(ts1);
Iterator itr = ts1.iterator();
while(itr.hasNext()){
Object element = itr.next();
System.out.println(element + "\n");
}
}
}
EXAMPLE 3
import java.util.Arrays;
import java.util.Comparator;
import java.io.*;
public class Filelistsort {
public static void main(String[] args) {
Comparator<File> byDirThenAlpha = new DirAlphaComparator();
Comparator<File> byNameLength = new NameLengthComparator();
File dir = new File(System.getProperty("user.home"));
File[] children = dir.listFiles();
System.out.println("Files by directory, then alphabetical");
Arrays.sort(children, byDirThenAlpha);
printFileNames(children);
System.out.println("Files by length of name (long first)");
Arrays.sort(children, byNameLength);
printFileNames(children);
}
private static void printFileNames(File[] fa){
for (File oneEntry : fa) {
System.out.println(" " + oneEntry.getName());
}
}
}
class DirAlphaComparator implements Comparator<File> {
public int compare(File filea, File fileb) {
if (filea.isDirectory() && !fileb.isDirectory()) {
return -1;
} else if (!filea.isDirectory() && fileb.isDirectory()) {
return 1;
} else {
return filea.getName().compareToIgnoreCase(fileb.getName());
}
}
}
class NameLengthComparator implements Comparator<File> {
public int compare(File filea, File fileb) {
int comp = fileb.getName().length() - filea.getName().length();
if (comp != 0) {
return comp;
} else {
return filea.getName().compareToIgnoreCase(fileb.getName());
}
}
}