如何按日期对字典数组进行排序?

时间:2016-02-25 07:26:32

标签: ios swift2 nsarray nsdate nsdictionary

我正在从Facebook获取一些帖子用于我的应用程序,我已将所有数据发布到字典中,并且该字典在数组中。现在我有来自Facebook的帖子时间,这是字典中的字符串。我想根据发布时间已知的最新帖子对该数组进行排序。我在SO上看过几个答案,但他们在一本字典中做了我想用不同的字典做的。 enter image description here

这是我的字典数组,我必须根据字典postTime值对数组进行排序。

代码:

var dateFormatter: NSDateFormatter = NSDateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                    var tempArray: NSMutableArray = NSMutableArray()
                    for var i = 0; i < dicts.count; i++ {
                        var dic: NSMutableDictionary = dicts[i] as! NSMutableDictionary
                        let dateConverted: NSDate = dateFormatter.dateFromString(dic["postTime"] as! String)!
                        dic["postTime"] = dateConverted
                        tempArray.addObject(dic)
                    }
                    var descriptor: NSSortDescriptor = NSSortDescriptor(key: "postTime", ascending: true)
                    var descriptors: NSArray = [descriptor]
                    var sortedArray: NSArray = tempArray.sortedArrayUsingDescriptors(descriptors as! [NSSortDescriptor])
                    NSLog("%@", sortedArray)

dicts是我的字典数组,我必须采用图片中显示的发布时间。

2 个答案:

答案 0 :(得分:0)

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"'Time: 'yyyy-MMM-dd HH:mm:ss";


    NSMutableArray *tempArray=[NSMutableArray new];
    for(NSInteger i=0;i<yourArrayOfDic.count ;i++){
        NSMutableDictionary *dic =[[NSMutableDictionary alloc] initWithDictionary:[yourArrayOfDic objectAtIndex:i]];
        NSDate *dateConverted = [dateFormatter dateFromString:[dic valueForKey:@"time"]];
        [dic setValue:dateConverted forKey:@"time"];
        [tempArray addObject:dic];
    }


    NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES];
    NSArray *descriptors=[NSArray arrayWithObject: descriptor];
    NSArray *sortedArray=[tempArray sortedArrayUsingDescriptors:descriptors];
    NSLog(@"%@",sortedArray);

//在斯威夫特......我很快就会变弱,但客观的快速可以帮助你。让我知道它的工作与否!!!!!

var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "'Time: 'yyyy-MMM-dd HH:mm:ss"
var tempArray: NSMutableArray = NSMutableArray()
for var i = 0; i < yourArrayOfDic.count; i++ {
    var dic: NSMutableDictionary = yourArrayOfDic[i]
    var dateConverted: NSDate = dateFormatter.dateFromString(dic["time"])
    dic["time"] = dateConverted
    tempArray.addObject(dic)
}
var descriptor: NSSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
var descriptors: NSArray = [descriptor]
var sortedArray: NSArray = tempArray.sortedArrayUsingDescriptors(descriptors)
NSLog("%@", sortedArray)

答案 1 :(得分:0)

IOS Swift 3.2

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
//   dateFormatter.locale = Locale.current
let tempArray: NSMutableArray = NSMutableArray()
for i in 0 ..< self.yourarr.count{
    var dicdata  = self.yourarr[i] as! [String : Any]

    var dateConverted = dateFormatter.date(from: dicdata["time"] as! String)
    dicdata["time"] = dateConverted
                    tempArray.add(dicdata)
 }
 var descriptor: NSSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
 var descriptors: NSArray = [descriptor]
 var sortedArray: NSArray = tempArray.sortedArray(using: descriptors as! [NSSortDescriptor]) as NSArray
 NSLog("%@", sortedArray)