How to sort based on/compare multiple values in Kotlin?
问题 Say I have a class Foo(val a: String, val b: Int, val c: Date) and I want to sort a list of Foo s based on all three properties. How would I go about this? 回答1: Kotlin's stdlib offers a number of useful helper methods for this. First, you can define a comparator using the compareBy() method and pass it to the sortedWith() extension method to receive a sorted copy of the list: val list: List<Foo> = ... val sortedList = list.sortedWith(compareBy({ it.a }, { it.b }, { it.c })) Second, you can