This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sorting vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting vectors" Watch "Sorting vectors" New topic
Author

Sorting vectors

John Brookes
Greenhorn

Joined: Feb 26, 2004
Posts: 29
I have a vector (LimitSet) of "Limit" objects, a limit consisting of an integer (pos) and a double (speed). How would I (if I even can) use Collections.sort to order the contents of the LimitSet by "pos"?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use the version of sort() that takes a java.util.Comparator as an argument. Provide a Comparator implementation which compares two Limit objects by subtracting their "pos" members and returning the result.


[Jess in Action][AskingGoodQuestions]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You could either modify the definition of LimitSet to implement Comparable or create a Comparator to use to call Collections.sort
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting vectors