Solved

# Sorting TreeMap by values

Posted on 2003-02-23
Medium Priority
4,358 Views
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
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

LVL 86

Expert Comment

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

Author Comment

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

ID: 8003450
Oh, i thought you meant by the integer key.

TreeSet ts = new TreeSet(new ValSorter());

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

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

LVL 86

Expert Comment

ID: 8003455
You can print the set by:

System.out.println(ts);
0

Author Comment

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

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

ID: 9059133
durban_posion:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Expert Comment

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

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