Sorting hashmap by values

How can I sort hasmap by values?And also I want to sort case-insensitive.What I mean is that I dont want sort first by upper-case letters and then lower-case letters for example Ag,Bg,Cg,Dg,af,bf,cf..., I want to sort like af, Ag, bf, Bg, cf, Cg...
[269 byte] By [BobMila] at [2007-11-26 14:10:40]
# 1

You can do Map.values(), put the resulting Collection in a List, and then sort the List.

If you want access to the keys, you can first create a reverse mapping, and then sort the keys of the reverse map, or better yet use a Map implementation that has sorted keys for the reverse map.

Or, you can do Map.entrySet(), put the result into a List, and then sort the List, using your own Comparator that sorts by values first and then by keys.

You can sort with Collections.sort().

To sort Strings without regard to case, use java.lang.String.CASE_INSENSITIVE_ORDER.

Most of the capitalized words above are classes or interfaces in the java.util package.

paulcwa at 2007-7-8 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 2
@BobMilI kind of agree with paul's answer but there is an interface defined... may be that can help u out http://java.sun.com/j2se/1.5.0/docs/api/java/util/SortedMap.html
RahulSharnaa at 2007-7-8 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 3
A SortedMap is what you'd use for the suggestion in the second sentence in my previous post. (..."use a Map implementation that has sorted keys for the reverse map")
paulcwa at 2007-7-8 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks, this works fine for me.

> You can do Map.values(), put the resulting Collection

> in a List, and then sort the List.

>

> If you want access to the keys, you can first create

> a reverse mapping, and then sort the keys of the

> reverse map, or better yet use a Map implementation

> that has sorted keys for the reverse map.

>

> Or, you can do Map.entrySet(), put the result into a

> List, and then sort the List, using your own

> Comparator that sorts by values first and then by

> keys.

>

> You can sort with Collections.sort().

>

> To sort Strings without regard to case, use

> java.lang.String.CASE_INSENSITIVE_ORDER.

>

> Most of the capitalized words above are classes or

> interfaces in the java.util package.

BobMila at 2007-7-8 1:58:19 > top of Java-index,Java Essentials,Java Programming...