zoukankan      html  css  js  c++  java
  • 误用 Kotlin 中的 sortedWith() 方法排序,集合没有变化

    时间: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() 方法来排序的。

  • 相关阅读:
    SSL JudgeOnline 1194——最佳乘车
    SSL JudgeOnline 1457——翻币问题
    SSL JudgeOnlie 2324——细胞问题
    SSL JudgeOnline 1456——骑士旅行
    SSL JudgeOnline 1455——电子老鼠闯迷宫
    SSL JudgeOnline 2253——新型计算器
    SSL JudgeOnline 1198——求逆序对数
    SSL JudgeOnline 1099——USACO 1.4 母亲的牛奶
    SSL JudgeOnline 1668——小车载人问题
    SSL JudgeOnline 1089——USACO 1.2 方块转换
  • 原文地址:https://www.cnblogs.com/ly570/p/11384655.html
Copyright © 2011-2022 走看看