• 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

Soriting a vector in descending order

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use TreeSet to store simply get an ordered list of Strings automatically!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic