【问题标题】:KOTLIN Compare n dimensional arrayKOTLIN 比较 n 维数组
【发布时间】:2020-09-10 05:33:03
【问题描述】:

我需要对列表列表进行排序... 我在 python 中有这样的东西:

a = sorted([[[1,2],[3,4]],[[1]]])

在 Kotlin 中有这样的东西吗? 我发现自定义比较器对象对于这样一个简单的任务来说真的很令人困惑。

【问题讨论】:

    标签: arrays list sorting kotlin


    【解决方案1】:

    如果我正确理解了 Python 的 sorted() 方法,这应该会产生相同的行为:

    fun main() {
        val a = listOf(listOf(listOf(1,2),listOf(4,3)), listOf(listOf(0,2,1)),listOf(listOf(1)))
        val b =a.sortedBy {it -> it.size}
        println(a)
        println(b)
    }
    

    输出:

    [[[1, 2], [4, 3]], [[0, 2, 1]], [[1]]]
    [[[0, 2, 1]], [[1]], [[1, 2], [4, 3]]]
    

    Python 等价物:

    a = [[[1,2],[4,3]], [[0,2,1]] ,[[1]]]
    b = sorted(a)
    print(a)
    print(b)
    

    输出:

    [[[1, 2], [4, 3]], [[0, 2, 1]], [[1]]]
    [[[0, 2, 1]], [[1]], [[1, 2], [4, 3]]]
    

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多