• 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

How to sort an vector of Object ?

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some Objects in my Vector.
I want to sort it by using comparable interface.
It works fine when i sort by "username" but when i want to sort by "username" AND "lastname" AND "date" it doesn't work.
When it's more then 1 criterion i have problems to do it,
can you help me? thanks.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing the objects in your vector are really some type that you made up with those fields. Going from there ... your comparator probably looks like:

Is that close? It needs some checks for nulls, instance of and such at the beginning to be very safe.

So now you want to sort by two or three fields. I do something like this:

Does that make sense? If the 1st field doesn't match I'm done. But if it does match I check the 2nd field. And if those match I check the 3rd field.

Note that I reversed myTwo and myOne in the 3rd test to sort descending order on that field. Does that look useful?
[ December 07, 2004: Message edited by: Stan James ]
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stan,

I would like to thank you very much. Now it works fine.
reply
    Bookmark Topic Watch Topic
  • New Topic