TreeMap sort by value

后端 未结 9 1084
闹比i
闹比i 2020-11-22 02:44

I want to write a comparator that will let me sort a TreeMap by value instead of the default natural ordering.

I tried something like this, but can\'t find out what

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 03:43

    A TreeMap is always sorted by the keys, anything else is impossible. A Comparator merely allows you to control how the keys are sorted.

    If you want the sorted values, you have to extract them into a List and sort that.

提交回复
热议问题