This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Address Book Sort by Ascending & Descending order

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do like this:
return name2.name.compareTo(name1.name);
Done.
Hope it helps.
Jason
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define a second Comparator, whose compare() function calls NAME.compare(o2, o1) .
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to just use Collections.reverse(List).
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all
I tried Collections.reverse(List) and it's working.
Thanks again,
Angela
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic