我正面临排序问题。场景解释为:我有一个类(假设人),它假设有4个属性(firstName,lastName,Address,salary)我正在创建它的对象并创建所有属性的集合并将此对象放在NSMutableArray中等等。所以在数组的每个索引上都有一个对象(即4个属性的集合)。现在我想根据工资对那个阵列进行排序,任何人都可以帮我解决这个问题,谢天谢地。,
答案 0 :(得分:0)
请参阅NSArray的文档。它有几种排序方法。搜索“排序”这个词,你就会发现它们。 (您需要基于块或基于功能的API)。
答案 1 :(得分:0)
使用此
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"salary" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[yorArrary sortUsingDescriptors:sortDescriptors];
答案 2 :(得分:0)
要么为对象实现比较方法:
- (NSComparisonResult)compare:(Person *)otherObject {
return [self.birthDate compare:otherObject.birthDate];
}
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
或通常甚至更好:( NSSortDescriptor的默认排序选择器是比较:)
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];