2

我有一个名为 nsmarrMedicineInfo 的 nsmutablearray。我想通过使用对元素进行排序NSSortDescriptor。当我执行以下代码时,它显示NSUnknownKeyException. 我找不到数组的键是什么。

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sort" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sort = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [nsmarrMedicineInfo sortedArrayUsingDescriptors:sort];
4

1 回答 1

1

Here key will be one of the property(key-value pair) of your custom object of array nsmarrMedicineInfo, on which you wish to sort your array. As in apple's doc:

An instance of NSSortDescriptor describes a basis for ordering objects by specifying the property to use to compare the objects, the method to use to compare the properties, and whether the comparison should be ascending or descending.

But if nsmarrMedicinInfo object consists of strings only then you can use sortedArrayUsingSelector method instead of sortDescriptor.

Thanks,

于 2011-11-29T04:44:09.993 回答