• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello troops

question: if i have a collection of objects, which are all the same, and the class has at least 3 different attributes. i want to sort this collection from time to time in respect to one of the attributes. the next time i sort it i want to do it in respect to a different attribute. whats the best way and most effecient way to do this?

thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would create an internal variable in the class definition which can be used in the compareTo method when you implement Comparable. You can then change that variable before you sort.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... or better, simply implement three different "Comparator" objects, and use those for sorting. All Java's "sort" methods accept an optional Compartor object to do the comparisons. This is likely to be simpler and less error-prone.
 
fred garvin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ernest. now it occurs to me that I may have done this before. would this be what you mean(or something similar):

Collections.sort(list, new Comparator(){
public int compare(Object obj1, Object obj2) {
String string1 = (String)obj1;
String string2 = (String)obj2;

return string2.length() - string1.length();
}
});
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, exactly.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,

One more thought on this:

What you showed was an anonymous inner class implementation of a Comparator. That works really well in some cases, and might be what you need. But, if you will be using the same type of sort in more than one place in your code, you would likely have this code in more than one place.

Just in case this was the only way you've seen Comparator implemented, realize it can be done in the "old fashioned" way as well:



and similarly for KeyTwo and KeyThree. Another thing I've seen done that I like is defining the different Comparators as final static members of the class they are used to sort:




Then, you can call the sort like this:



Hope this helps,
-- Jon
 
fred garvin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very elegant Jon, I will indeed steal your code and use it. Thank you.
[ February 18, 2006: Message edited by: fred garvin ]
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic