Martin
Martin

Reputation: 2863

How to sort the HashMap<String , Byte> by value in Android?

I am developing in Android , and I want to sort the the HashMap<String , Byte>. It print the all value like the following code and log:

for (Object key : groups.keySet()) {
    Log.d("addGroup", "groups.get(key) = " + groups.get(key) + " , key = " + key);

}

And the log show like the following:

groups.get(key) = -6 , key = iii
groups.get(key) = -5 , key = qqq
groups.get(key) = -3 , key = bbb
groups.get(key) = -7 , key = xxd
groups.get(key) = -2 , key = kmn
groups.get(key) = -4 , key = hhh
groups.get(key) = -12 , key = yhn
groups.get(key) = -9 , key = 987
groups.get(key) = -13 , key = zxc
groups.get(key) = -10 , key = hxt
groups.get(key) = -11 , key = tfx

How to sort the HashMap by value?

Thanks in advance.

Upvotes: 0

Views: 437

Answers (1)

aman shivhare
aman shivhare

Reputation: 334

  1. Pass your Hashmap to a Treemap
  2. Use the Comparator class to define your ordering

Have a look here Ordering HashMap

Upvotes: 1

Related Questions