• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to sort an vector of Object ?  RSS feed

 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Stan,

I would like to thank you very much. Now it works fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!