gpt4 book ai didi

arrays - 在 Swift 中订购二维数组

转载 作者:行者123 更新时间:2023-11-28 06:08:44 25 4
gpt4 key购买 nike

我尝试找到任何解决方案,但我找不到。

在 Swift 4 中,我有下一个数组:

[ ["839", "Huergo"], ["733", "Lavaisse"], ["732", "Pedro De Vega"]  ["824", "Obispo Boneo"],["727", "Hernandarias"], ["741", "Padre Genesio"], ["852", "Obispo Principe"]]

我需要按第二列的字母顺序排序,并作为最终数组获取:

[["727", "Hernandarias"], ["839", "Huergo"], ["733", "Lavaisse"], ["824", "Obispo Boneo"], ["852", "Obispo Principe"], ["741", "Padre Genesio"], ["732", "Pedro De Vega"]]

谢谢!

最佳答案

你可以简单地这样做:

let sortedArray = array.sorted { $0[1] < $1[1] }

看看 documentation sorted(by:) 方法,它说:

Returns the elements of the collection, sorted using the givenpredicate as the comparison between elements.

关于arrays - 在 Swift 中订购二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com