Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting MAP

 
Waez Ali
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am reading some data from properties file, & want sort this based on keys and in some cases based on values.

E.g test.properties

1 = Hi
2 = hello
3 = Bye
4 = Thank You
5 = Thank You
6 = Bye



When I load the above data into my properties object data is n more sorted.

Can someone please help me out to sort this data after reading it from properties file ?

Thanks in advance.
Waez Ali.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you want a java.util.TreeMap
Check the API
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeMap will sort the properties according to natural ordering of keys (unless you explicitly specify your own Comparator). If you want to have your properties in the order, in which they exist in file, I guess you would have to use Scanner class.
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw on the other post that you would like to sort properties by values without losing duplicate values. I don't know of any built-in API that could be used to accomplish this directly. You may use a kind of helper class, check out the example code:



But maybe there is an easier solution...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeMap keeps the entries of the map in the natural order of the keys (or in the order specified by a custom comparator) as already mentioned above.

If you want to keep the entries in the map in the order that they were added to the map, use LinkedHashMap (look it up in the API documentation for more info).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic