Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin sort hashmap in descending order

Tags:

People also ask

How do you sort HashMap in descending order?

In order to sort in decreasing order, just reverse the order of Comparator using Collections. reverseOrder() or Comparator. reverse() method of Java 8.

How does Kotlin sort in descending order?

To sort an Array in descending order in Kotlin, use Array. sortDescending() method. This method sorts the calling array in descending order in-place. To return the sorted array and not modify the original array, use Array.

Is Kotlin map ordered?

Yes, it will. The reference for mutableMapOf() says: The returned map preserves the entry iteration order. This is exactly the property that you asked about.


I have val myHashMap = HashMap<String, MutableList<TestItem>>(), hashmap key value is formatted date as a string for example 20-06-2018 how can I sort this hashMap in descending order?

expected result:

22-06-2018 : []
21-06-2018 : []
20-06-2018 : []

I use this code to sort it, but result is in ascending order:

val sortedMap = myHashMap.toSortedMap(compareBy { it })