如何按日期对字典数组进行排序?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何按日期对字典数组进行排序?相关的知识,希望对你有一定的参考价值。
我从Facebook获得了一些关于我的应用程序的帖子,我已经将所有数据发布到字典中,并且该字典在数组中。现在我有来自Facebook的帖子时间,这是字典中的字符串。我想根据发布时间已知的最新帖子对该数组进行排序。我在SO上看过几个答案,但他们在一本字典中做了我想用不同的字典做的。
这是我的字典数组,我必须根据字典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是我的字典数组,我必须采取图像中看到的发布时间。
答案
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)
另一答案
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)
以上是关于如何按日期对字典数组进行排序?的主要内容,如果未能解决你的问题,请参考以下文章