Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?
I have a treemap<Integer,Float>. How can I sort the the map on float point number?
Is there any quick way? or I have to write the comparator for the map?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In practice, you can’t (correctly, reliably) sort a
Mapimplementation by the values. (The implementations you might see claiming otherwise are hackish, unreliable, and behave really weirdly — rejecting duplicate values, throwing exceptions on attempts to look at keys not in the map, getting unrecoverably corrupted if the backing map changes…)Instead, sort the list of entries explicitly:
If you liked, you could then put this into a
LinkedHashMap: