Solved

java sorting list using data from key/value

Posted on 2012-03-15
6
239 Views
Last Modified: 2013-11-24
Hi,

I'm kinda in struggle here.
So I have a class Word which would just return Strings (but needed it for the equals method to equalsIgnoreCase())
I also have a WordScores class which containc a key/value collections of Words and scores (scores being the score of how much the word is being used)

Now in the WordScores class I have a method: sortByScore(java.util.List<Word> words)

the meaning would be to sort the list words by scores of the key/value collection. Also when the score is the same it should be sorted alphabetically.

Now I'm not sure what collection type to use for the key/value pair...
Also I hoped Collections.sort(words) would work to first sort it alphabetically but doesn't work as it doesn't know its a collection of objects containing only Strings.
0
Comment
Question by:Mutsop
  • 5
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37726440
You can use TreeMap, that will have your m,ap soprted buy the natural order of the keys:


http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37726455
So you can indeed suse TreeMap with word as key and score as value then it will keep them sorted all the time

Alternatively you should make sure the your class Word implemantes Comaprable interface or uyou can use method
Collections.sort(words, Comapartoer) to soerrt them
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37726464
In fact I think the best choice for you is to use metod

Collections.sort(words, Comparator) and depemdning on which comparaotr you use it will sort either by words or by the score with all the details inside - the way you'll define the method in your Comparator
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37726466
look at this linke to see an example how to use Comparoator:

http://www.vogella.de/blog/2009/08/04/collections-sort-java/
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37726483
So if you look at the code above you need to define your class comparator instead of MyIntComparaote, say

public class MyWordComparable implements Comparator<Word>{
 
    @Override
    public int compare(Word o1, Word o2) {
      // here you implement all the logic comparing scores and if they are identical compare words alphabetically
    }
}

Open in new window


then when you use Collactions.sort(..,...) with second parameter instance of this compartor you'll get sorted it the way you want

If you need towo different typoes of ordering then you can define two Comparotir classdes with different inmplementation of compareTo method and use the instance of the one you need in each particular case
0
 
LVL 3

Author Closing Comment

by:Mutsop
ID: 37742852
Wasn't suspecting so many answers from one use :)
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
Opening PDF on button click and fill new document 2 53
SHA2 certs for IIS AND Java? 2 121
JavaFX TableView not displaying correctly 3 102
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question