Solved

java sorting list using data from key/value

Posted on 2012-03-15
6
234 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now