NSSortDescriptor

Specifying Sorts Using NSSortDescriptor

 

 

Let’s assume, as an example, that we have an array (an instance of NSArray) containing instances of a custom class, Employee (that meets the requirements set out in “Requirements of Collection Objects”). The Employee class has attributes for an employee’s first and last name (instances of NSString), date of hire (an instance of NSDate), and age (an instance of NSNumber).

 

 

1:Sorting the array by the age key

ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"
                                           ascending:YES] autorelease];
sortDescriptors = [NSArray arrayWithObject:ageDescriptor];
sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];

 

2:Sorting the array by the age and date of hire key

ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"
                                                    ascending:YES] autorelease];
hireDateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"hireDate"
                                                     ascending:YES] autorelease];
sortDescriptors = [NSArray arrayWithObjects:ageDescriptor, hireDateDescriptor, nil];
sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];

 

 

Specifying Custom Comparisons

 

 

 Sorting the array using a localized case insensitive comparison

 

lastNameDescriptor = [[[NSSortDescriptor alloc]
              initWithKey:@"lastName"
              ascending:YES
              selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
 firstNameDescriptor = [[[NSSortDescriptor alloc]
              initWithKey:@"firstName"
              ascending:YES
              selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
 sortDescriptors = [NSArray arrayWithObjects:lastNameDescriptor,
              firstNameDescriptor, nil];
 sortedArray = [peopleArray sortedArrayUsingDescriptors:sortDescriptors];

 
The Foundation classes that have methods that can be used with sort descriptors are listed in Table 1.

 

Table 1  Common Foundation classes and comparison methods

 

 

 写道
Comparison Method Supporting Classes

compare: NSString, NSMutableString, NSDate, NSCalendarDate, NSValue (scalar types and unsigned char only), NSNumber
caseInsensitiveCompare: NSString, NSMutableString
localizedCompare: NSString, NSMutableString
localizedCaseInsensitiveCompare: NSString, NSMutableString
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值