关于NSSortDescriptor

来源:互联网 发布:网络监控光纤布线图 编辑:程序博客网 时间:2024/05/07 23:27
NSSortDescriptor由下述参数组成 :
键:对于一个给定的集合,对应值的键位将对集合中的每个对象进行排序。
升序:指定一个集合是否按照升序(YES)还是降序(NO)进行排序的布尔值。

另外NSSortDescriptor还有一个涉及到排序的值之间的比较的第三个可选参数。默认情况下,这是一个简单的相等性检查,但它的行为可以通过传递一个选择器(SEL)或者比较器(NSComparator)而发生改变。


任何时候当你在为面向用户的字符串排序时,一定要加入localizedStandardCompare:选择器,它将根据当前语言环境的语言规则进行排序(语言环境可能会根据大小写,变音符号等等的顺序而发生改变)。


有的集合(比如NSArray和NSSet)有以sortDescriptors作为参数,可以返回排过序的数组的方法。排序描述符按顺序应用,所以如果两个元素碰巧被捆绑在同一个特定的排序标准,束缚将被后续的任意描述符所打破。

NSSortDescriptor在Foundation和其他系统框架中随处可见,它在Core Data中发挥着极其重要的作用。如果你自己的类需要定义排序顺序,请按照惯例合理设置sortDescriptors参数。

因为,在现实中,你应该以商业逻辑来思考它,而不是用数学公式或者映射化简函数来考量它。在这一方面,NSSortDescriptor是一个大灌篮,以至于每当你走出Objective-C和Cocoa的世界时都会思念它。

//Person.h#import<Foundation/Foundation.h>@interface Person : NSObject@property(strong) NSString *firstName;@property(strong) NSString *lastName;@property(assign) int age;- (id)initWithFirstName:(NSSrting *)fName lastName:(NSString *)lName andAge:(int)a;- (void)reportState;@end

//Person.m#import "Person.h"@implementation Person@synthesize firstName, lastName, age;- (id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName andAge:(int)a{self = [super init];if(self){self.firstName = fName;self.lastName  = lName;self.age       = a;}return self;}- (void)reportState{NSLog(@"This person's name is %@ %@ who is %i years old", firstName,lastName,age);}@end 

#import <Foundation/Foundation.h>#import "Person.h"int main (int argc, const char * argv[]){@autoreleasepool {Person *p1 = [[Person alloc] initWithFirstName:@"Rebecca" lastName:@"Smith" andAge:33];Person *p2 = [[Person alloc] initWithFirstName:@"Albert" lastName:@"Case" andAge:24];Person *p3 = [[Person alloc] initWithFirstName:@"Anton" lastName:@"Belfey" andAge:45];Person *p4 = [[Person alloc] initWithFirstName:@"Tom" lastName:@"Gun" andAge:17];Person *p5 = [[Person alloc] initWithFirstName:@"Cindy" lastName:@"Lou" andAge:6];Person *p6 = [[Person alloc] initWithFirstName:@"Yanno" lastName:@"Dirst" andAge:76];NSArray *listOfObjects = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, p6, nil];NSLog(@"PRINT OUT ARRAY UNSORTED");[listOfObjects makeObjectsPerformSelector:@selector(reportState)];NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];NSSortDescriptor *sd3 = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:NO];NSArray *sdArray1 = [NSArray arrayWithObjects:sd1, sd2, sd3, nil];NSLog(@"PRINT OUT SORTED ARRAY (AGE, LASTNAME, FIRSTNAME)");NSArray *sortedArray1 = [listOfObjects sortedArrayUsingDescriptors:sdArray1];[sortedArray1 makeObjectsPerformSelector:@selector(reportState)];NSArray *sdArray2 = [NSArray arrayWithObjects:sd2, sd1, sd3, nil];NSArray *sortedArray2 = [listOfObjects sortedArrayUsingDescriptors:sdArray2];NSLog(@"PRINT OUT SORTED ARRAY (LASTNAME, FIRSTNAME, AGE)");[sortedArray2 makeObjectsPerformSelector:@selector(reportState)];}return 0;}                                                                         


0 0