kotlin list sort
时间: 2023-11-12 17:57:47 浏览: 71
要对 Kotlin 中的 List 进行排序,可以使用 `sorted()` 或 `sortedBy()` 函数。其中,`sorted()` 函数默认按照元素的自然顺序进行排序,而 `sortedBy()` 函数则需要传入一个 lambda 表达式,用于指定排序规则。
例如,对一个包含整数的 List 进行排序,可以使用以下代码:
```kotlin
val list = listOf(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
val sortedList = list.sorted()
println(sortedList) // 输出 [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
val sortedList2 = list.sortedBy { it % 3 }
println(sortedList2) // 输出 [3, 6, 9, 1, 1, 4, 2, 5, 5, 3, 5]
```
在第二个例子中,`sortedBy()` 函数的 lambda 表达式 `{ it % 3 }` 指定了按照元素对 3 取模的结果进行排序。
相关问题
kotlin sort
在Kotlin中,对集合进行排序非常方便。你可以使用`sortBy`函数按照指定的字段对集合进行升序排序,也可以使用`sortByDescending`函数对集合进行降序排序。另外,你还可以使用`sortWith`函数根据多个字段进行排序。
以下是一个示例代码,展示了如何使用`sortBy`、`sortByDescending`和`sortWith`函数对一个学生的集合进行排序:
```kotlin
data class Student(var name: String, var age: Int, var score: Int)
var list = mutableListOf<Student>()
list.add(Student("chenyu3", 23, 100))
list.add(Student("chenyu4", 24, 98))
list.add(Student("chenyu1", 21, 97))
list.add(Student("chenyu2", 22, 98))
list.add(Student("chenyu2", 20, 99))
// 按照年龄升序排序
list.sortBy { it.age }
println("按照年龄升序排序:")
list.forEach { println(it) }
// 按照年龄降序排序
list.sortByDescending { it.age }
println("按照年龄降序排序:")
list.forEach { println(it) }
// 先根据分数排序,如果分数相同再根据年龄排序
list.sortWith(compareBy({ it.score }, { it.age }))
println("先根据分数排序,如果分数相同再根据年龄排序:")
list.forEach { println(it) }
```
以上代码会输出按照不同排序规则排序后的学生集合。你可以根据自己的需求,使用相应的排序函数对集合进行排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Kotlin之集合排序(sortBy、sortByDescending)](https://blog.csdn.net/u011068702/article/details/119939256)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [Elements Of Kotlin Coroutines 0.1.pdf](https://download.csdn.net/download/jonny_zhang/11936482)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
kotlin sortwith
Kotlin中的`sortWith`函数可以根据指定的比较器(Comparator)对列表进行排序。以下是一个示例:
```kotlin
data class Person(val name: String, val age: Int)
fun main() {
val persons = listOf(Person("Alice", 29), Person("Bob", 31), Person("Charlie", 25))
val sortedList = persons.sortedWith(compareBy({ it.age }, { it.name }))
println(sortedList)
}
```
输出结果为:
```
[Person(name=Charlie, age=25), Person(name=Alice, age=29), Person(name=Bob, age=31)]
```
在上面的示例中,我们使用`sortedWith`函数对`persons`列表进行排序。`compareBy`函数可以根据指定的属性对对象进行排序。在这里,我们首先根据`age`属性进行排序,如果`age`相同,则根据`name`属性进行排序。最后将排序后的结果打印出来。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)