aspose file tools*
The moose likes Java in General and the fly likes Soriting a vector in descending order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Soriting a vector in descending order" Watch "Soriting a vector in descending order" New topic
Author

Soriting a vector in descending order

ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
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


In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 17, 2001
Posts: 213
You can also use TreeSet to store simply get an ordered list of Strings automatically!


Sarath Mohan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Soriting a vector in descending order