Comparator Interface has two methods compare(Obj o1, Obj o2) and equals(). equals method is also available in Object Class . so we override only compare() for custome shorting of multiple objects. in compare() we can pass multiple objects for comparision .
In this example i am sorting base on name, age and salary . so creating seprate class for every sorting criteria.
import java.util.*;
class Empl
{
String name;
int age;
double sal;
Empl(String name, int age, double sal)
{
this.name=name;
this.age=age;
this.sal=sal;
}
public String toString()
{
return "{"+name+", "+age+", "+sal+"}";
}
}
class AgeSorter implements Comparator
{
public int compare(Object o1, Object o2)
{
return ((Empl)o1).age-((Empl)o2).age;
}
}
class SalSorter implements Comparator
{
public int compare( Object o1, Object o2)
{
double s1=((Empl)o1).sal;
double s2=((Empl)o2).sal;
if(s1>s2)
{
return 1;
}
else if (s1<s2)
{
return -1;
}
else
{
return 0;
}
}
}
class NameSorter implements Comparator
{
public int compare(Object o1, Object o2)
{
return ((Empl)o1).name.compareTo(((Empl)o2).name);
}
}
public class TwoObjCompare
{
public static void main(String args[])
{
Empl[] e=new Empl[4];
e[0]=new Empl("Ajay", 32,234232);
e[1]=new Empl("Vijay", 53, 34324);
e[2]=new Empl("Pawan",34,35532);
e[3]=new Empl("Ram",43,45355.32);
for(Empl i:e)
{
System.out.println(i);
}
Scanner scn=new Scanner(System.in);
System.out.println("type sorting option (name/age/sal)");
String s=scn.next();
if(s.equals("age"))
{
Arrays.sort(e, new AgeSorter());
}
else if(s.equals("sal"))
{
Arrays.sort(e, new SalSorter());
}
else
{
Arrays.sort(e, new NameSorter());
}
System.out.println("after sorting "+s);
for(Empl i:e)
{
System.out.println(i);
}
}
}
In this example i am sorting base on name, age and salary . so creating seprate class for every sorting criteria.
import java.util.*;
class Empl
{
String name;
int age;
double sal;
Empl(String name, int age, double sal)
{
this.name=name;
this.age=age;
this.sal=sal;
}
public String toString()
{
return "{"+name+", "+age+", "+sal+"}";
}
}
class AgeSorter implements Comparator
{
public int compare(Object o1, Object o2)
{
return ((Empl)o1).age-((Empl)o2).age;
}
}
class SalSorter implements Comparator
{
public int compare( Object o1, Object o2)
{
double s1=((Empl)o1).sal;
double s2=((Empl)o2).sal;
if(s1>s2)
{
return 1;
}
else if (s1<s2)
{
return -1;
}
else
{
return 0;
}
}
}
class NameSorter implements Comparator
{
public int compare(Object o1, Object o2)
{
return ((Empl)o1).name.compareTo(((Empl)o2).name);
}
}
public class TwoObjCompare
{
public static void main(String args[])
{
Empl[] e=new Empl[4];
e[0]=new Empl("Ajay", 32,234232);
e[1]=new Empl("Vijay", 53, 34324);
e[2]=new Empl("Pawan",34,35532);
e[3]=new Empl("Ram",43,45355.32);
for(Empl i:e)
{
System.out.println(i);
}
Scanner scn=new Scanner(System.in);
System.out.println("type sorting option (name/age/sal)");
String s=scn.next();
if(s.equals("age"))
{
Arrays.sort(e, new AgeSorter());
}
else if(s.equals("sal"))
{
Arrays.sort(e, new SalSorter());
}
else
{
Arrays.sort(e, new NameSorter());
}
System.out.println("after sorting "+s);
for(Empl i:e)
{
System.out.println(i);
}
}
}