我有一个格式字典[Int:Any]我有一堆键值对。我想用毫秒时间格式整理字典
[169887: ["noti_type": 0, "project_name": "Design Project", "eventCount": 6, "author_pic": " /file/download/profile$2348d4f21095a01cc16a8ad9bf08f966.jpg", "canvas_name": "Design Reference #31 : Windows in NYC", "lastUpdatedAt": 1513585053629, "author_name": "Jake Kyung "],
173865: ["noti_type": 0, "project_name": "BeeCanvas DEV", "eventCount": 13, "author_pic": " &&&& /file/download/profile$80bbc9731a859d4e083df0df8044bcde.jpg &&&& /file/download/profile$2348d4f21095a01cc16a8ad9bf08f966.jpg", "canvas_name": "Android Release note", "lastUpdatedAt": 1513307315308, "author_name": " &&&& Sukho Bu &&&& Jake Kyung "]
我的词典中有很多这样的值。我想用lastUpdatedAt值对它们进行排序,这是1513585053629
毫秒格式。
我已经阅读了一些帖子,说如果我可以使用NSDate,这是可能的,
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"dateOfInfo" ascending: NO];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
但我只是想知道我是否可以使用毫秒格式而不是转换为NSDate直接实现这一目标?
答案 0 :(得分:1)
这只是理解的演示..
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc]init];
[dict1 setObject:@"Design Project" forKey:@"project_name"];
[dict1 setObject:@"1513585053629" forKey:@"lastUpdatedAt"];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
[dict2 setObject:@"BeeCanvas DEV" forKey:@"project_name"];
[dict2 setObject:@"1513307315308" forKey:@"lastUpdatedAt"];
NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr addObject:dict1];
[arr addObject:dict2];
NSLog(@"original array = %@",arr);
你可以在排序描述符中直接给出密钥名称。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"lastUpdatedAt" ascending:YES];
NSArray *sortedArray = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSLog(@"sorted array = %@",sortedArray);
<强>输出: - 强>
sorted array = (
{
lastUpdatedAt = 1513307315308;
"project_name" = "BeeCanvas DEV";
},
{
lastUpdatedAt = 1513585053629;
"project_name" = "Design Project";
}
)