【问题标题】:Getting keys from NSDictionary in ordered manner以有序的方式从 NSDictionary 获取密钥
【发布时间】:2018-10-07 22:14:42
【问题描述】:

我有这样的 JSON 响应:

 {
    "graphdata": {
        "2018-02-27 to 2018-03-05": {
            "historical": 2.93,
            "datewise": 2.82,
            "ordno": 3595,
            "revno": 89
        },
        "2018-03-06 to 2018-03-12": {
            "historical": 2.92,
            "datewise": 2.62,
            "ordno": 3780,
            "revno": 87
        },
        "2018-03-13 to 2018-03-19": {
            "historical": 2.92,
            "datewise": 3.16,
            "ordno": 3742,
            "revno": 86
        },
        "2018-03-20 to 2018-03-26": {
            "historical": 2.93,
            "datewise": 3.17,
            "ordno": 3745,
            "revno": 70
        },
        "2018-03-27 to 2018-04-02": {
            "historical": 2.93,
            "datewise": 3.08,
            "ordno": 4242,
            "revno": 84
        },
        "2018-04-03 to 2018-04-09": {
            "historical": 2.93,
            "datewise": 3.29,
            "ordno": 3575,
            "revno": 79
        },
        "2018-04-10 to 2018-04-16": {
            "historical": 2.94,
            "datewise": 3.19,
            "ordno": 3629,
            "revno": 69
        },
        "2018-04-17 to 2018-04-23": {
            "historical": 2.94,
            "datewise": 3.33,
            "ordno": 4211,
            "revno": 42
        },
        "2018-04-24 to 2018-04-30": {
            "historical": 2.94,
            "datewise": 0,
            "ordno": 1638,
            "revno": 0
        }
    }
}

现在,当我使用 allKeys 方法时,我会以这样的无序方式获取密钥:

(
    "2018-03-13 to 2018-03-19",
    "2018-04-03 to 2018-04-09",
    "2018-03-20 to 2018-03-26",
    "2018-04-10 to 2018-04-16",
    "2018-04-17 to 2018-04-23",
    "2018-04-24 to 2018-04-30",
    "2018-03-27 to 2018-04-02",
    "2018-02-27 to 2018-03-05",
    "2018-03-06 to 2018-03-12"
)

如何以有序的方式获取这些信息,例如开始应该是:“2018-02-27 到 2018-03-05”,

【问题讨论】:

  • 你可以使用排序描述符nshipster.com/nssortdescriptor这里有几个例子。
  • 字典总是中序格式,如果你需要顺序格式,那么使用Sort概念
  • shivam 你能分享那个数组迭代吗?你从 dict 中添加日期,因为我之前遇到过同样的问题。
  • @Anbu.karthik 感谢分享信息。它解决了我的问题。
  • @RB1509 我没明白你在问什么?

标签: ios objective-c json sorting nsdictionary


【解决方案1】:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
    NSDate *d1 = [df dateFromString: obj1];
    NSDate *d2 = [df dateFromString: obj2];
    return [d1 compare: d2];
}];

注意:根据你的日期设置日期格式

【讨论】:

    【解决方案2】:
    (((jsonDict.object(forKey: "graphdata") as! NSDictionary).allKeys) as! [String]).sorted(by: <) as NSArray
    

    这里 jsonDict 是你的父 NSDictionary

    【讨论】:

      【解决方案3】:

      每个字典都是键值对的无序集合。

      当您需要订购时使用DictionaryLiteral 实例 键值对的集合,不需要快速键查找 Dictionary 类型提供的。

      【讨论】:

      • 是否可以添加一些样本
      【解决方案4】:

      我使用 sortedArrayUsingComparator 解决了这个问题:

         NSArray *sortedKeys = [[myDictionary allKeys] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
              return [a compare:b];
          }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2023-03-13
        • 2010-11-20
        相关资源
        最近更新 更多