Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Sorting MAP by values
Waez Ali
Ranch Hand
Posts: 43
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
posted Today 1:50 PM
--------------------------------------------------------------------------------
Hi,
I am opening this again with specific question.
I am reading some data from properties file, & need to sort the map by values.
E.g test.properties
1 = Hi
2 = hello
3 = Bye
4 = Thank You
5 = Thank You
6 = Bye
I tried with following
public static Map getSortedMap(Map map,boolean sortOrder){ Map sortedMap = new LinkedHashMap(); if(map!=null){ List mapKeys = new ArrayList(map.keySet()); List mapValues = new ArrayList(map.values()); //Sort the values Collections.sort(mapValues); Object[] sortedArray = mapValues.toArray(); int size = sortedArray.length; if(sortOrder){//Ascending order //It sorts the data in ascending order. for (int i=0; i<size; i++){ sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])), sortedArray[i]); } } else{//Descending sort for (int i=size; i>0 :wink: { sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[--i])), sortedArray[i]); } } } return sortedMap; }
Problem with this code is it overrides the duplicate values.
Is there any other way I can sort the data, please suggest.
Thanks in advance.
Waez Ali.
[ July 21, 2008: Message edited by: Waez Ali ]
Paul Sturrock
Bartender
Posts: 10336
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Your original question was moved
here
. Please continue any discussion there, rather than starting a new topic.
[ July 21, 2008: Message edited by: Paul Sturrock ]
JavaRanch FAQ
HowToAskQuestionsOnJavaRanch
Consider Paul's
rocket mass heater
.
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
sorting hashmap based on values
displaytag help, sorting map objects
How to sort a Map with Enums
Retrieving different keys when the values in a hashmap are same???
Stupid case insensitive sorting
More...