How to sort the elements of a list in reverse order ?

Using Collections.reverseOrder():
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList ts=new ArrayList();
ts.add(7);
ts.add(3);
ts.add(1);
ts.add(10);
ts.add(9);
Collections.sort(ts);
System.out.println(ts);
}};
Using our custom Comparator:
import java.util.*;
class demo implements Comparator
{
public int compare(Integer i1,Integer i2){
return i2.compareTo(i1);
}
public static void main(String[] args){
ArrayList ts=new ArrayList();
ts.add(7);
ts.add(3);
ts.add(1);
ts.add(10);
ts.add(9);
Collections.sort(ts,new demo());
System.out.println(ts);
}};

Can sort() can sort user defined objects(Address in the previous ex)
Consider this example
import java.util.*;
class Address
{
String name;
int age;
String city;
Address(String name,int age,String city){
this.name=name;
this.age=age;
this.city=city;}
public String toString(){
return "name:"+name+"\tage:"+age+"\tcity:"+city+"\n";
}
};
class demo
{
public static void main(String[] args){
ArrayList
al=new ArrayList
();
Address add1=new Address("bhabani",24,"blore");
Address add2=new Address("mahesh",23,"kolkata");
Address add3=new Address("pinku",22,"bbsr");
Address add4=new Address("chary",24,"chennai");
al.add(add1);
al.add(add2);
al.add(add3);
al.add(add4);
Collections.sort(al);
System.out.println(al);
}};
Now you will find compilation error like this
demo.java:27: cannot find symbol
symbol : method sort(java.util.ArrayList
)
location: class java.util.Collections
Collections.sort(al);
^
1 error
Now consider this
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList ts=new ArrayList();
ts.add(7);
ts.add(3);
ts.add(1);
ts.add(10);
ts.add(9);
Collections.sort(ts);
System.out.println(ts);
}};
Now it runs fine.This is because Sort() details
Public static>void sort(List list)
It sorts the specified list into ascending order according to the natural ordering of the elements.
All elements in the list must implement the comparable interface. And all elements in the list must be mutually comparable (i.e. e1.compareTo(e2) must not throw ClassCastException for any elements e1 and e2).
So the sort() can take only lists of Comparable Objects. Address is not a subclass of Comparable, so you can not sort() the Address.

So we can make Address subclass of Comparable so that the sort() will work.
Here the sorting is according to the name.
import java.util.*;
class Address implements Comparable

{
String name;
int age;
String city;
Address(String name,int age,String city){
this.name=name;
this.age=age;
this.city=city;}
public String getname(){return name;}
public String toString(){
return "name:"+name+"\tage:"+age+"\tcity:"+city+"\n";
}
public int compareTo(Address add){
return name.compareTo(add.getname());
}};
class demo
{
public static void main(String[] args){
ArrayList
al=new ArrayList
();
Address add1=new Address("bhabani",24,"blore");
Address add2=new Address("mahesh",23,"kolkata");
Address add3=new Address("pinku",22,"bbsr");
Address add4=new Address("chary",24,"chennai");
al.add(add1);
al.add(add2);
al.add(add3);
al.add(add4);
Collections.sort(al);
System.out.println(al);
}};

Using custom comparater
In the above prog if you want to compare the objects by age or by city then,,,
Here invoking sort(List l,Comparater c) it can be done.
That means the list element’s compareTo() will not be called, and the Comparater’s compare method will be called. That means the elements in the list do not need to implement the Comparable interface.

We have to make sub class of Comparator and pass the instance of the class in the sort().
import java.util.*;
class Address
{
String name;
Integer age;
String city;
Address(String name,Integer age,String city){
this.name=name;
this.age=age;
this.city=city;}
public String getname(){return name;}
public Integer getage(){return age;}
public String getcity(){return city;}
public String toString(){
return "name:"+name+"\tage:"+age+"\tcity:"+city+"\n";
}};
class Namecompare implements Comparator

{
public int compare(Address one,Address two){
return one.getname().compareTo(two.getname());
}};
class Citycompare implements Comparator

{
public int compare(Address one,Address two){
return one.getcity().compareTo(two.getcity());
}};
class Agecompare implements Comparator

{
public int compare(Address one,Address two){
return one.getage().compareTo(two.getage());
}};
class demo
{
public static void main(String[] args){
ArrayList
al=new ArrayList
();
Address add1=new Address("bhabani",24,"blore");
Address add2=new Address("mahesh",23,"kolkata");
Address add3=new Address("pinku",20,"bbsr");
Address add4=new Address("chary",21,"chennai");
al.add(add1);
al.add(add2);
al.add(add3);
al.add(add4);
Namecompare nc=new Namecompare();
Collections.sort(al,nc);
System.out.println(al);
Citycompare cc=new Citycompare();
Collections.sort(al,cc);
System.out.println(al);
Agecompare ac=new Agecompare();
Collections.sort(al,ac);
System.out.println(al);
}};

No comments:

Post a Comment