aspose file tools*
The moose likes Java in General and the fly likes Address Book Sort by Ascending & Descending order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Address Book Sort by Ascending & Descending order" Watch "Address Book Sort by Ascending & Descending order" New topic
Author

Address Book Sort by Ascending & Descending order

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
I have java application, to sort names of address book in Ascending order. I used following code for that:
Collections.sort(set,SortEngine.NAME);
In SortEngine is class:
public static final Comparator NAME = new Comparator()
{
public int compare(Object o1, Object o2)
{
try
{
SortEngine name1 = (SortEngine)o1;
SortEngine name2 = (SortEngine)o2;
return name1.name.compareTo(name2.name);
}
catch(ClassCastException e)
{
// do something here
}
return 0;
}
// do something for equals too
};
I only did for Ascending order. How to do in Descening order? so, user can do sort by Ascending and Descending order.
Thanks,
Angela
Jason Wang
Greenhorn

Joined: Apr 13, 2001
Posts: 26
Do like this:
return name2.name.compareTo(name1.name);
Done.
Hope it helps.
Jason
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Define a second Comparator, whose compare() function calls NAME.compare(o2, o1) .


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You may also want to just use Collections.reverse(List).


"I'm not back." - Bill Harding, Twister
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Thanks to all
I tried Collections.reverse(List) and it's working.
Thanks again,
Angela
 
 
subject: Address Book Sort by Ascending & Descending order
 
Similar Threads
i have not override comparator's equals() method(overrided compare())-working fine-how?
need to find quicksort that sorts in descending order ..ARG
Sorting the Rows in a JTable Component Based on a Column
Sorting through Comparable(Very Urgent)
Sorting