使用 NSDictionary 对 NSArray 进行排序
我正在使用 NSSortDiscriptors 对 NSArray 进行排序。 数组中的每个索引都包含一个带有键、值条目的字典。这本词典还包含另一本词典。
我想要解决的问题是根据第一个字典上的值对数组进行排序,同时也根据它包含的字典中的一些值对数组进行排序。
以下是我对数组中其他值使用的排序方法。
- (void)sortArray {
if ([array count] != 0) {
// Reports sortingDescriptors
NSSortDescriptor * city = [[NSSortDescriptor alloc] initWithKey:@"City" ascending:YES];
NSSortDescriptor * name = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
NSSortDescriptor * country = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES];
[reportsArray sortUsingDescriptors:[NSArray arrayWithObjects:city, name, country, nil]];
[name release];
[city release];
[country release];
}
}
该数组看起来像这样:
[{name = "";
city = "";
country = "";
date = {
dateAdded = "";
dateRemoved = "";
}
}];
例如,如果在 dateAdded 上有值,我也想进行排序。
I'm using NSSortDiscriptors to sort an NSArray.
Every index in the array contains a dictionary with key,value entries. This dictionary also holds another dictionary.
My problem that I want solve is to sort the array based on values on the first dictionary, but also on some values in the dictionary it contains.
The following is the sort method I use for the other values in the array.
- (void)sortArray {
if ([array count] != 0) {
// Reports sortingDescriptors
NSSortDescriptor * city = [[NSSortDescriptor alloc] initWithKey:@"City" ascending:YES];
NSSortDescriptor * name = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
NSSortDescriptor * country = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES];
[reportsArray sortUsingDescriptors:[NSArray arrayWithObjects:city, name, country, nil]];
[name release];
[city release];
[country release];
}
}
The array looks like this:
[{name = "";
city = "";
country = "";
date = {
dateAdded = "";
dateRemoved = "";
}
}];
So I also want to sort on, if have value on dateAdded for example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在创建
NSSortDescriptor
时指定关键路径,这样您就可以使用NSDictionary
对NSArray
进行排序。You can specify a key path when you create the
NSSortDescriptor
, this way you can sort theNSArray
with aNSDictionary
.您可能想检查字典是否包含这样的值:
然后您需要对字典之外的剩余对象进行排序,为此,请通过执行以下操作来复制没有字典条目的数组副本:
You might want to check if the dictionary contains a value like that :
Then you need to sort remaining objects but the dictionary, to do so, make a copy of your array without the dictionary entry by doing something like :
您是说数组中的对象具有 City、Name 和 Country 属性,而且还有字典属性,并且您想要对字典中的键之一进行排序?或者你是说数组中的条目是字典,但有时城市、名称或国家键丢失?或者你是说有些条目是字典,有些是具有列出属性的对象?
在任何情况下,您都可以通过使用 initWithKey:ascending:comparator:。这允许您提供一个比较器块作为排序函数,它比直接选择器更灵活,例如
将为您提供一个排序描述符,该描述符对数组进行排序,将所有 NSDictionaries 首先,然后再排序其他对象。 (
self
是所有返回对象本身的NSObject
所拥有的键)。Are you saying that the objects in the array have a City, Name and Country property but also a dictionary property and you want to sort on one of the keys in the dictionary? Or are you saying that the entries in the array are dictionaries but sometimes the City, Name or Country key is missing? Or are you saying that some of the entries are dictionaries and some are objects with the listed properties?
In any case, you can get more flexibility by creating a sort descriptor using initWithKey:ascending:comparator:. This allows you to supply a comparator block as the sort function which is more flexible than a straight selector e.g.
will give you a sort descriptor that sorts the array putting all the NSDictionaries first and other objects afterwards. (
self
is a key possessed by allNSObject
s that returns the object itself).