0

I have a Linkedhasmap and have key and values

Now i want to sort the map by values and the key and value should appear as follows

Example,
 Key    Value
  1      0.0
  2      0.2
  3      0.5
  4      0.0
  5      0.3
  6      0.0
  7      0.1

Expected output
  1      0.0
  4      0.0
  6      0.0
  7      0.1
  2      0.2
  5      0.3
  3      0.5

Please advise

Thanks

  • 1
    Does this answer your question? [Sort a Map by values](https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values) – Jeroen Steenbeeke May 21 '21 at 08:42
  • No. I am talking about sorting the linkedhashmap – dolphin trendz May 21 '21 at 12:47
  • LinkedHashMap always has insertion order. This cannot be changed. Ergo you need to create a new LinkedHashMap with items inserted by increasing value. This can be done using the steps in the question and answers I linked. – Jeroen Steenbeeke May 21 '21 at 12:53

1 Answers1

0

You cannot sort on Linkedhasmap, but you can put entries into the List and sort the List. After clearing Linkedhasmap, you can put your entries.

Görhem
  • 16