Solved

java sorting list using data from key/value

Posted on 2012-03-15
6
238 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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