我想做的事情似乎很简单,但我在网上找不到任何答案。我有一个对象的NSMutableArray,假设它们是Person对象。我想对NSMutableArray按Person排序。birthDate是一个NSDate。
我认为这与这个方法有关:
NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(???)];
在Java中,我将使我的对象实现Comparable,或使用集合。使用内联自定义比较器排序…在Objective-C中怎么做呢?
我在iOS 4中使用了block。
必须将数组的元素从id转换为类类型。
在本例中,它是一个名为Score的类,其属性为points。
此外,你需要决定做什么,如果你的数组的元素不是正确的类型,对于这个例子,我只是返回NSOrderedSame,但在我的代码中,我虽然一个异常。
NSArray *sorted = [_scores sortedArrayUsingComparator:^(id obj1, id obj2){
if ([obj1 isKindOfClass:[Score class]] && [obj2 isKindOfClass:[Score class]]) {
Score *s1 = obj1;
Score *s2 = obj2;
if (s1.points > s2.points) {
return (NSComparisonResult)NSOrderedAscending;
} else if (s1.points < s2.points) {
return (NSComparisonResult)NSOrderedDescending;
}
}
// TODO: default is the same?
return (NSComparisonResult)NSOrderedSame;
}];
return sorted;
PS:这是降序排序。
如果你只是排序一个nsnumber数组,你可以用一个调用来排序它们:
[arrayToSort sortUsingSelector: @selector(compare:)];
这是因为数组中的对象(NSNumber对象)实现了比较方法。你可以为NSString对象做同样的事情,甚至为实现了比较方法的自定义数据对象数组做同样的事情。
下面是一些使用比较器块的示例代码。它对字典数组进行排序,其中每个字典在键“sort_key”中包含一个数字。
#define SORT_KEY @\"sort_key\"
[anArray sortUsingComparator:
^(id obj1, id obj2)
{
NSInteger value1 = [[obj1 objectForKey: SORT_KEY] intValue];
NSInteger value2 = [[obj2 objectForKey: SORT_KEY] intValue];
if (value1 > value2)
{
return (NSComparisonResult)NSOrderedDescending;
}
if (value1 < value2)
{
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
上面的代码演示了如何为每个排序键获取整数值并对它们进行比较。因为NSNumber对象实现了一个比较方法,它可以重写得更简单:
#define SORT_KEY @\"sort_key\"
[anArray sortUsingComparator:
^(id obj1, id obj2)
{
NSNumber* key1 = [obj1 objectForKey: SORT_KEY];
NSNumber* key2 = [obj2 objectForKey: SORT_KEY];
return [key1 compare: key2];
}];
或者比较器的主体甚至可以被提炼为一行:
return [[obj1 objectForKey: SORT_KEY] compare: [obj2 objectForKey: SORT_KEY]];
我倾向于使用简单的语句和大量的临时变量,因为代码更容易阅读,也更容易调试。编译器无论如何都会优化掉临时变量,因此,全在一行中的版本没有任何优势。
参见NSMutableArray方法sortUsingFunction:context:
您将需要设置一个compare函数,该函数接受两个对象(类型为Person,因为您正在比较两个Person对象)和一个上下文参数。
这两个对象只是Person的实例。第三个对象是字符串,例如@"birthDate"。
这个函数返回一个NSComparisonResult:如果是PersonA,则返回NSOrderedAscending。birthDate < person .birthDate。它将返回nsordereddescent如果PersonA。person .birthDate。最后,它将返回NSOrderedSame如果PersonA。birthDate == person .birthDate。
这是粗略的伪代码;你需要充实一个日期“更少”、“更多”或“等于”另一个日期的含义(比如比较从epoch开始的秒数等):
NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) {
if ([firstPerson birthDate] < [secondPerson birthDate])
return NSOrderedAscending;
else if ([firstPerson birthDate] > [secondPerson birthDate])
return NSOrderedDescending;
else
return NSOrderedSame;
}
如果你想要一些更紧凑的东西,你可以使用三元运算符:
NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) {
return ([firstPerson birthDate] < [secondPerson birthDate]) ? NSOrderedAscending : ([firstPerson birthDate] > [secondPerson birthDate]) ? NSOrderedDescending : NSOrderedSame;
}
内联可能会加快这一点,如果你这样做了很多。