/*********  方法2 简单类型对象的排序   ************/

if 0

// 创建排序对象
// 参数1 - 为简单类型对象可填写nil/@"self"
//        为复杂类型对象可填写要进行排序的关键字, 如 按照Person的name成员排序 可填写@"name";
//参数2 - 升序/降序(YES/NO)
NSMutableArray *arrA = [NSMutableArray arrayWithObjects:@"2", @"4", @"6", @"1", nil];
NSSortDescriptor *sortD = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
// 排序方法
// 注意参数需要填写数组的类型对象  数组中需要放置描述排序的对象
[arrA sortUsingDescriptors:[NSArray arrayWithObject:sortD]];
NSLog(@"%@", arrA);

endif

屏幕快照 2016-01-07 下午4.51.41.png

 /*************** 方法2复杂类型对象的排序 ************/

if 1

Person *per11 = [[Person alloc] initWithName:@"Long" age:22];
Person *per22 = [[Person alloc] initWithName:@"Bian" age:18];
Person *per33 = [[Person alloc] initWithName:@"YI" age:20];

NSMutableArray *arrP = [NSMutableArray arrayWithObjects:per11, per22, per33, nil];
NSSortDescriptor *sortD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];// 年龄排序
[arrP sortUsingDescriptors:[NSArray arrayWithObject:sortD]];
for (Person *peraa in arrP) {
    NSLog(@"%@ %ld", peraa.name, peraa.age);
}

endif

屏幕快照 2016-01-07 下午4.52.04.png

**Person.h
Person.m 对象的参考代码请点击下面链接**
Person.h Person.m 代码