[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Sorting TreeMap by values Part 2

Asked by johngamble in Java Programming Language

Tags: treemap, sort, value

I founding an existing question that I am interested in having the answer to , however the proposed solution appears to sort by the key and not by the value.  How do you go about sorting by the value?

Below is the question/solution I am referencing.

 Solution Title: Sorting TreeMap by values
asked by durban_posion on 02/23/2003 08:17AM PST  
This solution was worth  50 Points and received a grade of A  
 


Hi there,
How do I sort TreeMap by integer values?

TreeMap example:

           Map map = new TreeMap(comp);
           map.put("1", "1000");
           map.put("6", "4000");
           map.put("8", "3000");
           map.put("25", "10000");
           map.put("13", "5000");
           map.put("19", "6000");

Result should be:

key             value
1               1000
8               3000
6               4000
13              5000
19              6000
25             10000  

Thanks DP
 

 
 Send to a Friend     Printer Friendly   See Solution  
   
 Comment from CEHJ
Date: 02/23/2003 08:19AM PST
 Comment  


It's already sorted by that value
 
Comment from durban_posion
Date: 02/23/2003 08:32AM PST
 Author Comment  


It is not

if u iterate this map result will be sorted by keys, not values.

1               1000
6               4000
8               3000
13              5000
19              6000
25             10000  

What I need is something that sotrs this map by values (1000,3000,4000, etc)

But thanks anyway for quick respond
 
Comment from CEHJ
Date: 02/23/2003 08:50AM PST
 Comment  


Oh, i thought you meant by the integer key.

    TreeSet ts = new TreeSet(new ValSorter());
    ts.addAll(map.values());

..............

class ValSorter implements Comparator {
  public int compare(Object o1, Object o2) {
    return new Integer((String)o1).compareTo(new Integer((String)o2));
  }
}

 
Comment from CEHJ
Date: 02/23/2003 08:51AM PST
 Comment  


You can print the set by:

System.out.println(ts);
 
Comment from durban_posion
Date: 02/23/2003 09:07AM PST
 Author Comment  


I also need keys.
Map must be sorted by values but must contain a key/value pair not just value as in your example.

Result should be:
key             value
1               1000
8               3000
6               4000
13              5000
19              6000
25             10000  

Thanks
 
Accepted Answer from CEHJ
Date: 02/23/2003 09:18AM PST
Grade: A
 Accepted Answer  


You have two alternatives - don't use Strings as keys as you've done or create the map using the class i gave you:

Map map = new java.util.TreeMap(new ValSorter());

 
Comment from CleanupPing
Date: 08/03/2003 03:00AM PDT
 Comment  


durban_posion:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
 
Administrative Comment from AnnieMod
Date: 11/12/2003 04:00AM PST
 Administrative Comment  


Force accepted

AnnieMod
Community Support Moderator @ Experts-Exchange
 

 
 
   
 
[+][-]05/11/05 06:41 PM, ID: 13983366Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Java Programming Language
Tags: treemap, sort, value
Sign Up Now!
Solution Provided By: aozarov
Participating Experts: 3
Solution Grade: A
 
[+][-]05/12/05 02:55 AM, ID: 13984619Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07/27/05 07:45 AM, ID: 14537264Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/27/05 08:04 AM, ID: 14537476Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/18/05 12:56 AM, ID: 14698579Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/22/05 10:06 AM, ID: 14726035Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81