1.reverse
反转
package com.example.kotlin01
val list = listOf(1,2,3,5,6,7)
fun main() {
val result = list.reversed()
println(result)
}
[7, 6, 5, 3, 2, 1]
2.sort
排序,从小到大
package com.example.kotlin01
val list = listOf(1,2,3,5,6,7)
fun main() {
val result = list.sorted()
println(result)
}
[1, 2, 3, 5, 6, 7]
3.sortBy
根据某个函数处理后在排序,从小到大
package com.example.kotlin01
val list1 = listOf(2, 4, 1, 9, 5, 10)
fun main() {
val result = list1.sortedBy { it/-1 }
println(result)
}
[10, 9, 5, 4, 2, 1]
4.sortDescending
排序,从大到小
package com.example.kotlin01
val list1 = listOf(2, 4, 1, 9, 5, 10)
fun main() {
val result = list1.sortedDescending()
println(result)
}
[10, 9, 5, 4, 2, 1]
5.sortDescendingBy
根据某个函数处理后在排序,从大到小
package com.example.kotlin01
val list1 = listOf(2, 4, 1, 9, 5, 10)
fun main() {
val result = list1.sortedByDescending { it / -1 }
println(result)
}
[1, 2, 4, 5, 9, 10]