【问题标题】:how to sort [json] use SwiftyJSON in swift 4.x?如何排序 [json] 在 swift 4.x 中使用 SwiftyJSON?
【发布时间】:2018-12-16 15:24:23
【问题描述】:
import SwiftyJSON

let myData: [JSON] = [{"number": "1"}, {"number": "4"}, {"number": "3"}]

let sortedData = myData.sorted { $0["number"].stringValue > $1["number"].stringValue }
print(sortedData) // nil

如何在不更改字典的情况下进行排序?

我想按值高低排序。

数组中的值不是JSON吗?

我只是认为数组值与它们的输入无关,因为数组起到了排序的作用。

我错了吗?

【问题讨论】:

    标签: ios json swift swifty-json


    【解决方案1】:

    为 SwiftyJSON 声明 JSON 字符串时需要使用方括号。

    sorted 将返回已排序的元素,不会修改原始内容。

    let myData: [JSON] = [["number": "1"], ["number": "4"], ["number": "3"]]
    let sortedData = myData.sorted { $0["number"].stringValue > $1["number"].stringValue }
        print(sortedData)
    

    【讨论】:

    • mydata 不是我定义的。这只是一个例子。它是从服务器接收到的数据。打印时不是方括号。
    • 我刚刚使用了您使用大括号的示例。使用方括号对数据进行排序。您是否有从服务器获得的样本?你怎么称呼它?
    • 对不起。数据接近错误。我知道的方式是对的。
    猜你喜欢
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多