-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathNSArray+SortDescriptorsHelp.m
42 lines (32 loc) · 1.24 KB
/
NSArray+SortDescriptorsHelp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "NSArray+SortDescriptorsHelp.h"
@implementation NSArray (SortByKey)
+ (NSArray*)sortDescriptorsForStringValueForKey:(NSString*)key {
NSSortDescriptor* sortDescriptor ;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] ;
NSArray* sortDescriptors ;
sortDescriptors = [NSArray arrayWithObject:sortDescriptor] ;
[sortDescriptor release] ;
return sortDescriptors ;
}
- (NSArray*)arraySortedByStringValueForKey:(NSString*)key {
return [self sortedArrayUsingDescriptors:[NSArray sortDescriptorsForStringValueForKey:key]] ;
}
- (NSArray*)arraySortedByKeyPath:(NSString*)keyPath {
if (!keyPath) {
keyPath = @"description" ;
}
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:keyPath
ascending:YES] ;
NSArray* orderedArray = [self sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release] ;
return orderedArray ;
}
@end
@implementation NSMutableArray (SortByKey)
- (void)sortByStringValueForKey:(NSString*)key {
NSArray* sortDescriptors = [NSArray sortDescriptorsForStringValueForKey:key] ;
[self sortUsingDescriptors:sortDescriptors] ;
}
@end