?
Solved

Sorting TreeMap by values

Posted on 2003-02-23
9
Medium Priority
?
4,358 Views
Last Modified: 2007-12-19
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
 
0
Comment
Question by:durban_posion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8003319
It's already sorted by that value
0
 

Author Comment

by:durban_posion
ID: 8003371
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8003450
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));
  }
}
0
Industry Leaders: 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 86

Expert Comment

by:CEHJ
ID: 8003455
You can print the set by:

System.out.println(ts);
0
 

Author Comment

by:durban_posion
ID: 8003522
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
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8003564
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());
0
 

Expert Comment

by:CleanupPing
ID: 9059133
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.
0
 

Expert Comment

by:johngamble
ID: 13982599
I am interested in the solution to this problems as well, however the propsoed solution appears to sort by the key and not by the value.  How do you go about sorting by the value?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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