在Swift中排序字典数组

时间:2017-06-26 14:43:42

标签: ios swift

我从响应中得到一个数组,如下所示

  items=[{
         "displayName": "Adam Codo",
         "postedTime": "2011-04-04T21:31:20.000Z",
         "message" : "Hello Test Message"
         },
         {
         "displayName": "Leena",
         "postedTime": "2011-04-04T20:32:20.000Z",
         "message" : " Start Hello Test Message"
         },
         {
         "displayName": "Zohn",
         "postedTime": "2011-04-03T22:47:20.000Z",
         "message" : "Hi Leena"
         },
         {
         "displayName": "Leena",
         "postedTime": "2011-04-04T21:32:20.000Z",
         "message" : " Start Hello Test Message"
         },
         {
         "displayName": "Adam Codo",
         "postedTime": "2011-04-04T22:13:10.000Z",
         "message" : "Hello Test Message"
         }]

我按照名称排序数组&后来的时间。所以我需要排序数组如下

  items=[
         {
         "displayName": "Adam Codo",
         "postedTime": "2011-04-04T22:13:10.000Z",
         "message" : "Hello reply Test Message"
         },
         {
         "displayName": "Adam Codo",
         "postedTime": "2011-04-04T21:31:20.000Z",
         "message" : "Hello Test Message"
         },
         {
         "displayName": "Leena",
         "postedTime": "2011-04-04T21:32:20.000Z",
         "message" : " Start Hello Test Message"
         },
         {
         "displayName": "Leena",
         "postedTime": "2011-04-04T20:32:20.000Z",
         "message" : " Start Hello Test Message"
         },
         {
         "displayName": "Zohn",
         "postedTime": "2011-04-03T22:47:20.000Z",
         "message" : "Hi Leena"
         }
]

有谁能建议我怎么做?任何想法都会很棒。

3 个答案:

答案 0 :(得分:2)

请勿使用词典

建立自己的模型,生活会更轻松。

这是模型

struct Element {
    let displayName: String
    let postedTime: Date
    let message: String

    init?(dict:[String:String]) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
        guard
            let displayName = dict["displayName"],
            let timeText = dict["postedTime"],
            let postedTime = dateFormatter.date(from: timeText),
            let message = dict["message"]
            else { return nil }

        self.displayName = displayName
        self.postedTime = postedTime
        self.message = message
    }
}

现在让它成为Comparable

extension Element: Comparable {

    static func <(lhs: Element, rhs: Element) -> Bool {
        if lhs.displayName != rhs.displayName {
            return lhs.displayName < rhs.displayName
        }
        return lhs.postedTime < rhs.postedTime
    }

    static func ==(lhs: Element, rhs: Element) -> Bool {
        return lhs.displayName == rhs.displayName && lhs.postedTime < rhs.postedTime
    }

}

现在给出你的词典数组

let items = [
    [
        "displayName": "Adam Codo",
        "postedTime": "2011-04-04T21:31:20.000Z",
        "message" : "Hello Test Message"
    ],
    [
        "displayName": "Leena",
        "postedTime": "2011-04-04T20:32:20.000Z",
        "message" : " Start Hello Test Message"
    ],
    [
        "displayName": "Zohn",
        "postedTime": "2011-04-03T22:47:20.000Z",
        "message" : "Hi Leena"
    ],
    [
        "displayName": "Leena",
        "postedTime": "2011-04-04T21:32:20.000Z",
        "message" : " Start Hello Test Message"
    ],
    [
        "displayName": "Adam Codo",
        "postedTime": "2011-04-04T22:13:10.000Z",
        "message" : "Hello Test Message"
    ]
]

我们可以将它转换为Element(s)数组并最终对其进行排序

let sortedElms = items.flatMap(Element.init).sorted()

结果

[
    Element(displayName: "Adam Codo", postedTime: 2011-04-04 21:31:20 +0000, message: "Hello Test Message"),
    Element(displayName: "Adam Codo", postedTime: 2011-04-04 22:13:10 +0000, message: "Hello Test Message"),
    Element(displayName: "Leena", postedTime: 2011-04-04 20:32:20 +0000, message: " Start Hello Test Message"),
    Element(displayName: "Leena", postedTime: 2011-04-04 21:32:20 +0000, message: " Start Hello Test Message"),
    Element(displayName: "Zohn", postedTime: 2011-04-03 22:47:20 +0000, message: "Hi Leena")
]

答案 1 :(得分:1)

var sortedResults = items.sorted {
          (dictOne, dictTwo) -> Bool in 

    if dictOne["displayName"]! != dictTwo["displayName"]! {
            return dictOne["displayName"]! < dictTwo["displayName"]!
        }

        return dictOne["postedTime"]! < dictTwo["postedTime"]!
        };

答案 2 :(得分:0)

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey: @"displayName" ascending: YES];

NSArray *sortedArray = [_locations sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

NSArray *sortedLocations = [_locations sortedArrayUsingComparator: ^(NSDictionary *d1, NSDictionary *d2) {
    ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
    NSDate *theDate1 = [formatter dateFromString:[d1 valueForKey:@"postedTime"]];
    NSDate *theDate2 = [formatter dateFromString:[d2 valueForKey:@"postedTime"]];
    return [theDate1 compare:theDate2];
}];

NSArray *sortedLocations = [_locations sortedArrayUsingComparator: ^(NSDictionary *d1, NSDictionary *d2) {

    ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
    NSDate *theDate1 = [formatter dateFromString:[d1 valueForKey:@"postedTime"]];
    NSDate *theDate2 = [formatter dateFromString:[d2 valueForKey:@"postedTime"]];

    NSString *name1 = [d1 valueForKey:@"displayName"];
    NSString *name2 = [d2 valueForKey:@"displayName"];

    if ([name1 compare:name2]) {
        return [theDate1 compare:theDate2];
    }


    return NSOrderedDescending;
}];

但是我没有检查它