• 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 using comparator interface

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object that contains basically 2 fields: a String and an integer. When I sort for display I want to be able to sort both ways, alphabetically and numerically. Right now, inside the object I have written an equals(Object o) method for the aphabetical display. I wrote my own quickSort for numerical display. Should I just get rid of the equals method and write 2 comparator classes? I'm not exactly sure how to use comparator, or if it even applies here, but it seems right.
Someone give me some help, please!
Micah
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be on the right track. Assuming you have a List of your objects, you could write two Comparator classes, one for sorting by integers & the other one by Strings. Then you could use the following method passing in the appropriate Comparator instance:

You could define the comparator class as:

You get the general idea...

[This message has been edited by Junaid Bhatra (edited August 02, 2001).]
 
Micah Wedemeyer
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, got it working with Collections.sort() Heh, it's even faster than my quickSort was. I guess I'll have to brush up on my sorting optimizations.
Micah
 
reply
    Bookmark Topic Watch Topic
  • New Topic