Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option, define your own wrapper class for Strings:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic