Solved

java sorting list using data from key/value

Posted on 2012-03-15
6
237 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 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