按NSDate排序字典数组 [英] Sort Array of Dictionaries by NSDate

查看:86
本文介绍了按NSDate排序字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列字典。在每个字典中,有一个键 dateOfInfo (一个 NSDate )和其他一些东西。我想按每个词典 dateOfInfo 对数组进行排序,最近的结果是第一个结果。

I have an array of dictionaries. Within each dictionary, there is a a key dateOfInfo (an NSDate) and several other things. I want to sort the array by each dictionaries dateOfInfo with the most recent being the first result.

我该怎么做?

推荐答案

你可以使用排序 NSSortDescription ,例如

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"dateOfInfo" ascending: NO];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];

您还可以使用方法

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

这篇关于按NSDate排序字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆