NSSortDescriptor

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 中,可以使用 `NSSortDescriptor` 类来对 `UITableView` 中的数据进行排序。以下是一个示例,假设我们有一个包含 `Person` 对象的数组 `peopleArray`,每个 `Person` 对象都有一个 `name` 属性和一个 `age` 属性。 首先,我们需要创建一个 `NSSortDescriptor` 对象来指定我们要按照哪个属性进行排序: ```swift let nameSortDescriptor = NSSortDescriptor(key: "name", ascending: true) let ageSortDescriptor = NSSortDescriptor(key: "age", ascending: true) ``` 上面的代码创建了两个 `NSSortDescriptor` 对象,一个按照 `name` 属性升序排序,另一个按照 `age` 属性升序排序。 接下来,我们需要将这些 `NSSortDescriptor` 对象传递给 `sortedArray(using:)` 方法来对数组进行排序。在这个例子中,我们将使用 `nameSortDescriptor` 对数组进行排序: ```swift let sortedPeopleArray = (peopleArray as NSArray).sortedArray(using: [nameSortDescriptor]) as! [Person] ``` 上面的代码将 `peopleArray` 数组按照 `name` 属性升序排序,结果存储在 `sortedPeopleArray` 数组中。如果你想按照多个属性排序,只需将 `NSSortDescriptor` 对象放入数组中即可: ```swift let sortedPeopleArray = (peopleArray as NSArray).sortedArray(using: [nameSortDescriptor, ageSortDescriptor]) as! [Person] ``` 上面的代码将 `peopleArray` 数组按照 `name` 属性升序排序,如果有相同的 `name` 属性,则按照 `age` 属性升序排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值