您的位置:首页 > 其它

NSSortDescriptor

2015-12-15 11:58 295 查看
NSSortDescriptor可以实现按照对象的属性进行排序.

NSSortDescriptor有下述参数组成 :

键(key):对于一个给定的集合,对应值的键位将对集合中的每个对象进行排序。

升序(ascending ):指定一个集合是否按照升序(YES)还是降序(NO)进行排序的布尔值。

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

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

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

使用NSSortDescriptor的情况:

There are a number of situations in which you can use sort descriptors, for example:
To sort an array (an instance of NSArray or NSMutableArray—see sortedArrayUsingDescriptors: and sortUsingDescriptors:).
To directly compare two objects (see compareObject:toObject:)
To specify how the elements in a table view should be arranged (see sortDescriptors).
To specify how the elements managed by an array controller should be arranged (see sortDescriptors)
If you are using Core Data, to specify the ordering of objects returned from a fetch request (see sortDescriptors)


以下是几种使用NSSortDescriptor的不同组合来将它们排序的方法:

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property NSString *firstName;
@property NSString *lastName;
@property NSNumber *age;

@end

#import "Person.h"

@implementation Person

//这里决定了排序之后的打印对象信息的输出.
- (NSString *)description {
return [NSString stringWithFormat:@"%@ %@ %@", self.firstName, self.lastName,self.age];
}

@end

//1: 利用上面自定义对象(Person)。
NSArray *firstNameArray = @[ @"Alice", @"Bob", @"Charlie", @"Quentin", @"Jack" ];
NSArray *lastNameArray = @[ @"Smith", @"Jones", @"Smith", @"Alberts", @"Tomas" ];
NSArray *ageArray = @[ @24, @27, @33, @31,@16 ];

NSMutableArray *personMutableArray = [NSMutableArray array];

[firstNameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

Person *person =[[Person alloc]init];

person.firstName = firstNameArray[idx];
person.lastName = lastNameArray[idx];
person.age = ageArray[idx];

[personMutableArray addObject:person];

}];

//按开始名字排序
NSSortDescriptor *firstNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES selector:@selector(localizedStandardCompare:)];

//按最后名字排序
NSSortDescriptor *lastNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES selector:@selector(localizedStandardCompare:)];

//按年龄排序
NSSortDescriptor *ageSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];

NSLog(@"By age: %@", [personMutableArray sortedArrayUsingDescriptors:@[ageSortDescriptor]]);
/*
By age: (
"Charlie Smith 33",
"Quentin Alberts 31",
"Bob Jones 27",
"Alice Smith 24",
"Jack Tomas 16"
)

*/

NSLog(@"By first name: %@", [personMutableArray sortedArrayUsingDescriptors:@[firstNameSortDescriptor]]);
/*
By first name: (
"Alice Smith 24",
"Bob Jones 27",
"Charlie Smith 33",
"Jack Tomas 16",
"Quentin Alberts 31"
)
*/

//设置两个或者以上的排序条件.根据添加顺序进行先后排序.如果lastName遇到相同的会立马按照然first name进行排序.
NSLog(@"By last name, first name: %@", [personMutableArray sortedArrayUsingDescriptors:@[lastNameSortDescriptor, firstNameSortDescriptor]]);

/*
By last name, first name: (
"Quentin Alberts 31",
"Bob Jones 27",
"Alice Smith 24",
"Charlie Smith 33",
"Jack Tomas 16"
)
*/


利用 NSSortDescriptor 对 NSMutableArray 排序的一个场景应用

有时我们在NSMutableArray中存的是网络请求返回的数据,而每一个元素又是一个NSDictionary,如果这时候需要把数组中的元素按照每个元素字典中某一个key来排序,那么我们可以利用NSSortDescriptor来快速实现需求。

-(NSMutableArray *) changeArray:(NSMutableArray *)dicArray orderWithKey:(NSString *)key ascending:(BOOL)yesOrNo{

NSSortDescriptor *distanceDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:yesOrNo];
[dicArray sortUsingDescriptors:@[distanceDescriptor]];

return dicArray;
}


IOS中文排序

http://www.cnblogs.com/syxchina/archive/2012/10/11/2720257.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: