Creating and Using Sort Descriptors
It is important to remember that NSSortDescriptor does not sort objects. It provides the description of how to sort objects. The actual sorting is done by other classes, often NSArray or NSMutableArray.
Specifying Sorts Using NSSortDescriptor
ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES] autorelease];
sortDescriptors = [NSArray arrayWithObject:ageDescriptor];
sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];Each of the sort descriptors are applied in sequence, providing a means of sorting on multiple property keys.
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];
In each of these cases, the default comparison method, compare:, is used.
Specifying Custom Comparisons
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.

Sample Code
+ (NSArray *)knownRegions
{
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
NSMutableArray *regions = [[NSMutableArray alloc] initWithCapacity:timeZoneNames.count];
for (NSString *timeZoneName in timeZoneNames){
NSArray *nameComponents = [timeZoneName componentsSeparatedByString:@"/"];
NSString *regionName = [nameComponents objectAtIndex:0];
Region *region = nil;
for (Region *aRegion in regions){
if ([aRegion.name isEqualToString:regionName])
region = aRegion;
}
if (region == nil){
region = [[Region alloc] initWithRegionName:regionName];
[regions addObject:region];
[region release];
}
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName];
TimeZoneWrapper *timeZoneWrapper = [[TimeZoneWrapper alloc] initWithTimeZone:timeZone nameComponents:nameComponents];
[region.timeZoneWrappers addObject:timeZoneWrapper];
[timeZoneWrapper release];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[regions sortUsingDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
for (Region *aRegion in regions){
[aRegion sortTimeZoneWrappers];
}
return [regions autorelease];
}
本文详细介绍了如何使用NSSortDescriptor对Objective-C中的对象进行排序,包括指定排序方式、应用多个排序属性以及自定义比较方法。通过示例代码展示了在不同场景下实现排序的方法。
14

被折叠的 条评论
为什么被折叠?



