wood burning stoves 2.0*
The moose likes Java in General and the fly likes Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting" Watch "Sorting" New topic
Author

Sorting

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Hi all,
Actually it is hard to explain here but i hope someone can give me an idea. I have several Strings and points for each String. (I'm trying to sort Strings according to their (search) points)
I can not add points as key and Strings as value into TreeMap because it is possible that i can have same points for different Strings.
How can i sort these collection of Strings according to their points?
Thanks in advance.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Do you have the strings in an array or in a list? You can sort an array using Arrays.sort(...) and a list using Collections.sort(...). There are versions of those methods that take a Comparator. The Comparator is an object that decides the order during the sorting.

You could write your own implementation of interface Comparator and use it to sort the strings according to the order you desire.

Here you can find examples of how to write a Comparator: Comparator examples


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

You could write your own implementation of interface Comparator and use it to sort the strings according to the order you desire.

I think this is what i need.
Thanks for the tip.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
A few more links complements of google:

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
http://www.onjava.com/pub/a/onjava/2003/03/12/java_comp.html


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Another option, define your own wrapper class for Strings:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting
 
Similar Threads
Sorting
Sorting java.lang.objects
Sorting of vector
sorting an ArrayList Alphabetically
Clear My Doubt