Kotlin sort

Define Class

1
2
3
4
5
data class Person(
var name: String = "",
var age: Int = 0,
var money: Double = 0.0
)

sortBy

1
2
3
4
5
6
7
8
9
10
11
12
val persons = mutableListOf<Person>()

persons.add(Person("Hello-1", 1, 10.0))
persons.add(Person("Hello-5", 5, 20.0))
persons.add(Person("Hello-3", 3, 20.0))
persons.add(Person("Hello-2", 2, 100.0))

persons.forEach(::println)
println("=========================")
persons.sortBy { it.age }

persons.forEach(::println)
1
2
3
4
5
6
7
8
9
Person(name=Hello-1, age=1, money=10.0)
Person(name=Hello-5, age=5, money=20.0)
Person(name=Hello-3, age=3, money=20.0)
Person(name=Hello-2, age=2, money=100.0)
=========================
Person(name=Hello-1, age=1, money=10.0)
Person(name=Hello-2, age=2, money=100.0)
Person(name=Hello-3, age=3, money=20.0)
Person(name=Hello-5, age=5, money=20.0)

sortBy 为正序排列,跟其对应的降序方法为 sortByDescending。


sortWith

1
2
3
4
5
6
7
8
9
10
11
12
13
val persons = mutableListOf<Person>()

persons.add(Person("Hello-1", 1, 10.0))
persons.add(Person("Hello-5", 5, 20.0))
persons.add(Person("Hello-3", 3, 20.0))
persons.add(Person("Hello-2", 2, 100.0))
persons.add(Person("Hello-6", 2, 50.0))

persons.forEach(::println)
println("=========================")
persons.sortWith(compareBy({ it.age }, { it.money }))

persons.forEach(::println)
1
2
3
4
5
6
7
8
9
10
11
Person(name=Hello-1, age=1, money=10.0)
Person(name=Hello-5, age=5, money=20.0)
Person(name=Hello-3, age=3, money=20.0)
Person(name=Hello-2, age=2, money=100.0)
Person(name=Hello-6, age=2, money=50.0)
=========================
Person(name=Hello-1, age=1, money=10.0)
Person(name=Hello-6, age=2, money=50.0)
Person(name=Hello-2, age=2, money=100.0)
Person(name=Hello-3, age=3, money=20.0)
Person(name=Hello-5, age=5, money=20.0)

sortWith 方法中,传入的 compareBy({属性1},{属性2},…)参数,参数个数是可变的。但是都是默认的升序排列。

可以给sortWith传入一个Comparator对象,来达到一个更高级更复杂的逻辑。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
val persons = mutableListOf<Person>()

persons.add(Person("Hello-1", 1, 10.0))
persons.add(Person("Hello-5", 5, 20.0))
persons.add(Person("Hello-3", 3, 20.0))
persons.add(Person("Hello-2", 2, 100.0))
persons.add(Person("Hello-6", 2, 50.0))

persons.forEach(::println)
println("=========================")
val comparator: Comparator<Person> = Comparator { o1, o2 ->
if (o2.age == o1.age) {
o1.money.compareTo(o2.money)
} else {
o1.age - o2.age
}
}
persons.sortWith(comparator)
// persons.sortWith(compareBy({ it.age }, { it.money }))
persons.forEach(::println)

Comparable

1
2
3
4
5
6
7
8
9
10
11
12
13
data class Person(
var name: String = "",
var age: Int = 0,
var money: Double = 0.0
) : Comparable<Person> {
override fun compareTo(other: Person): Int {
return if (other.age == this.age) {
this.money.compareTo(other.money)
} else {
this.age - other.age
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
val persons = mutableListOf<Person>()

persons.add(Person("Hello-1", 1, 10.0))
persons.add(Person("Hello-5", 5, 20.0))
persons.add(Person("Hello-3", 3, 20.0))
persons.add(Person("Hello-2", 2, 100.0))
persons.add(Person("Hello-6", 2, 50.0))

persons.forEach(::println)
println("=========================")
persons.sort()
persons.forEach(::println)