• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Address Book Sort by Ascending & Descending order

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do like this:
return name2.name.compareTo(name1.name);
Done.
Hope it helps.
Jason
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Define a second Comparator, whose compare() function calls NAME.compare(o2, o1) .
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all
I tried Collections.reverse(List) and it's working.
Thanks again,
Angela
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic