File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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: 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.

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

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!
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:
subject: Soriting a vector in descending order
It's not a secret anymore!