时间:2019年8月4日14:17:06
问题描述:
看下边的小例子:

data class Man(val name: String, val age: Int, val type: Int)

fun main(args: Array<String>) {
val list = mutableListOf<Man>()
list.add(Man("wzc", 31,2))
list.add(Man("wzj", 32,1))
list.add(Man("wcx", 3,1))
list.add(Man("wcg", 7,1))
println("before sort")
for (man in list) {
println(man)
}
list.sortedWith(Comparator {lh, rh ->
if (lh.type.compareTo(rh.type) == 0) {
lh.age.compareTo(rh.age)
} else {
lh.type.compareTo(rh.type)
}
})
println("after sort")
for (man in list) {
println(man)
}
}

/*
打印结果:
before sort
Man(name=wzc, age=31, type=2)
Man(name=wzj, age=32, type=1)
Man(name=wcx, age=3, type=1)
Man(name=wcg, age=7, type=1)
after sort
Man(name=wzc, age=31, type=2)
Man(name=wzj, age=32, type=1)
Man(name=wcx, age=3, type=1)
Man(name=wcg, age=7, type=1)
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
可以看到排序前后,打出的内容没有丝毫变化。
解决方法:
看一下 sortedWith 的代码:

/**
* Returns a list of all elements sorted according to the specified [comparator].
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> {
if (this is Collection) {
if (size <= 1) return this.toList()
@Suppress("UNCHECKED_CAST")
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}
return toMutableList().apply { sortWith(comparator) }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
可以排序后的结果是在返回值里面。
修改代码:

data class Man(val name: String, val age: Int, val type: Int)

fun main(args: Array<String>) {
val list = mutableListOf<Man>()
list.add(Man("wzc", 31,2))
list.add(Man("wzj", 32,1))
list.add(Man("wcx", 3,1))
list.add(Man("wcg", 7,1))
println("before sort")
for (man in list) {
println(man)
}
// list.sortedWith(Comparator {lh, rh ->
// if (lh.type.compareTo(rh.type) == 0) {
// lh.age.compareTo(rh.age)
// } else {
// lh.type.compareTo(rh.type)
// }
// })
// println("after sort")
// for (man in list) {
// println(man)(http://www.my516.com)
// }
val sortedWith = list.sortedWith(Comparator { lh, rh ->
if (lh.type.compareTo(rh.type) == 0) {
lh.age.compareTo(rh.age)
} else {
lh.type.compareTo(rh.type)
}
})
list.clear()
list.addAll(sortedWith)
println("after sort")
for (man in list) {
println(man)
}
}

/*
打印结果:
before sort
Man(name=wzc, age=31, type=2)
Man(name=wzj, age=32, type=1)
Man(name=wcx, age=3, type=1)
Man(name=wcg, age=7, type=1)
after sort
Man(name=wcx, age=3, type=1)
Man(name=wcg, age=7, type=1)
Man(name=wzj, age=32, type=1)
Man(name=wzc, age=31, type=2)
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
可以看到,正常排序了。可以看到还有个 sortWith 方法:

expect fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit
1
二者的区别是:sortedWith() 方法可以通过 Iterable 对象调用,排序结果在返回值里;而 sortWith() 方法只能通过 MutableList 来调用,排序结果不在返回值里,而是直接在调用对象里了。sortedWith() 方法内部最终还是调用 sortWith() 方法来排序的。

误用 Kotlin 中的 sortedWith() 方法排序,集合没有变化的更多相关文章

  1. str中的join方法; set集合;深浅拷贝

    一.str中的join方法 1,用join可以吧列表转换为字符串 将列表转换成字符串. 每个元素之间用_拼接 s = "_". join(['德玛', ''赵信'', '易']) ...

  2. 千万不要误用 java 中的 HashCode 方法

    刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...

  3. java中Collections.sort()方法实现集合排序

    1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>();   //定 ...

  4. 关于Kotlin中日志的使用方法

    1 引言 想必学过Java的人都知道一个@Slf4j使用得多么的舒服: @Slf4j public class TestController{ @GetMapping("/test" ...

  5. Day07_39_集合中的remove()方法 与 迭代器中的remove()方法

    集合中的remove()方法 与 迭代器中的remove()方法 深入remove()方法 iterator 中的remove()方法 collection 中的remove(Object)方法 注意 ...

  6. 【转载】 C#中List集合使用OrderByDescending方法对集合进行倒序排序

    在C#的List集合操作中,有时候需要针对List集合进行排序操作,如果是对List集合按照元素对象或者元素对象的某个属性进行倒序排序的话,可以使用OrderByDescending方法来实现,Ord ...

  7. Kotlin中功能操作与集合(KAD 11)

    作者:Antonio Leiva 时间:Feb 2, 2017 原文链接:https://antonioleiva.com/functional-operations-collections-kotl ...

  8. 对list集合中的对象进行排序(转载)

    原文链接:http://blog.csdn.net/veryisjava/article/details/51675036 Collections对List集合中的数据进行排序 有时候需要对集合中的元 ...

  9. hashCode()方法以及集合中Set的一些总结

    一.前言 本篇文章没有什么主题,就是一些零散点的总结.周末没事看了几道蚂蚁金服的面试题,其中有好几道都是特别简单的,基础性的题目,就是我们平时用到的,但是发现要是完全说出来还是有一些不清楚的地方,所以 ...

随机推荐

  1. fatal: refusing to merge unrelated histories问题解决

    git中pull或merge时偶尔出现

  2. mysql 表相关操作(1)

    查询语句 select * from t_deptselect empno,ename,sal from t_emp select    empno,   sal * 12 as "inco ...

  3. VeryNginx故障排除

    在安装和使用 VeryNginx 的过程中可能会遇到一些问题,下面列举了常见的问题及对应的解决方案,供参考. Q: run "python instal.py install all&quo ...

  4. java springmvc poi 导出Excel,先简单记录,后期会详细描写

    POI jar包下载 : http://poi.apache.org/download.html jsp代码 <%@ page language="java" content ...

  5. C#下载图片,用户选择保存路径

    Html代码 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></ti ...

  6. 启动tomcat出现org.springframework.web.servlet.DispatcherServlet错误

    项目右键 properites ==> deployment Assembly => add lib包

  7. Java 迪杰斯特拉算法实现查找最短距离

    迪杰斯特拉算法 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是 ...

  8. Redis5以上版本伪集群搭建(高可用集群模式)

    redis集群需要至少要三个master节点,我们这里搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,这里用一台机器(可以多台机器部署,修改一下ip地 ...

  9. Redis启动方式

    一.前端启动 直接运行bin/redis-server将以前端模式启动.[bin目录是在/usr/local/redis/bin] # ./redis-server 启动缺点: ssh命令窗口关闭则r ...

  10. Vue示例教程

    <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml&q ...