• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Soriting a vector in descending order

 
ernest fakudze
Ranch Hand
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All! Please help!!
I have a vector which holds String objects. I need to sort these strings in descending order. I read the docs and I gather that I have to use the Collections interface. I also read this previous thread: http://www.javaranch.com/ubb/Forum34/HTML/001752.html and I'm still confused about using a Comparator.
Please provide code to do this if you have time. I would do it for you if the situation was reversed.
Thanks in advance.
Ernie
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not the java.util.Collection Interface, but the java.util.Collections Class. (confusing, isn't it?)
The interface defines some behaviour for all classes that fit in the Collections Framework, the class defines some helper functionality for doing some common things easily.
A Comparator defines how to create an absolute ordering of another class type, but since a String already has an absolute order (namely alphabetical order ) you don't have to re-invent anything.
Collections has a sort(List) method, and a Vector is a List so you can pass it in and the Vector will be sorted (in one line of code!) You can either iterate through the Vector backwards, or pass the Vector to the Collections.reverse(List) and it will pass the reversed list to you.
Two lines of code!
Dave.
 
Sarath Mohan
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use TreeSet to store simply get an ordered list of Strings automatically!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic