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

ruarlubt  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(180)

我从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是我的字典数组,我必须采取后的时间,这是在图像中看到的。

ybzsozfc

ybzsozfc1#

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);

//在Swift中..我在Swift中很弱,但是客观的C到Swift可能会帮助你。让我知道它是否有效!!!!!

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)
6bc51xsx

6bc51xsx2#

IOS斯威夫特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)

相关问题