【问题标题】:Swift: Sort array with dictionary insideSwift:对数组进行排序,里面有字典
【发布时间】:2015-07-07 14:34:39
【问题描述】:

我有这个数组:

[[Lots: 2783, Development: Suntec City], [Lots: 1583, Development: Marina Square], [Lots: 594, Development: The Esplanade], [Lots: 1270, Development: Millenia Singapore], [Lots: 283, Development: Singapore Flyer], [Lots: 185, Development: Orchard Point], [Lots: 152, Development: The Heeren], [Lots: 595, Development: Plaza Singapura], [Lots: 77, Development: The Cathay], [Lots: 302, Development: Mandarin Hotel], [Lots: 1092, Development: Ngee Ann City], [Lots: 331, Development: Wisma Atria], [Lots: 1251, Development: VivoCity P3], [Lots: 933, Development: Sentosa], [Lots: 369, Development: Tang Plaza], [Lots: 932, Development: Harbourfront Centre], [Lots: 196, Development: Far East Plaza], [Lots: 324, Development: Centrepoint], [Lots: 218, Development: Concorde Hotel], [Lots: 434, Development: ION Orchard], [Lots: 194, Development: 313@Somerset], [Lots: 1971, Development: Resorts World Sentosa], [Lots: 726, Development: VivoCity P2], [Lots: 268, Development: Orchard Central], [Lots: 386, Development: Park Mall], [Lots: 812, Development: Marina Bay Sands]]

我想按字母顺序按“发展”值对它们进行排序。我试过使用 sorted by 似乎它只适用于字典

【问题讨论】:

  • 请显示您尝试过的实际代码。给我们一些开始。

标签: ios arrays swift dictionary


【解决方案1】:

这应该对你有帮助:

array.sort({ $0.Development > $1.Development })

this

这是比较第一个参数和第二个参数的闭包的缩写。 (假设两者都是字符串。)(你can compare strings using >。)

【讨论】:

  • 我收到一个错误,无法使用类型为 '((,)->_)' 的参数列表调用 'sort'
  • @Eneres 我刚刚遇到了同样的问题,但fliter。我仍在寻找解决方案。如果我找到了,我会和你分享。
  • @Eneres 请参阅my question。答案也应该可以解决您的问题。
  • 抱歉,我是 swift 新手。不知道如何将它应用到我的数组:(
【解决方案2】:
DataArray.sort {
                item1, item2 in
                let development1 = item1["Development"]! as String
                let development2 = item2["Development"]! as String
                return development1 > development2
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    相关资源
    最近更新 更多