-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathNSArray+sort.h
66 lines (51 loc) · 1.87 KB
/
NSArray+sort.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#import <Foundation/NSArray.h>
@interface NSArray (YOLOSort)
/**
Returns the receiver, sorted.
id rv = @[@2, @1, @3, @5, @4].sort;
// rv => @[@1, @2, @3, @4, @5]
We even try to handle mixed object types:
id rv = @[@"20", @"1", @3, @5, @"4"].sort;
// rv => @[@"1", @3, @"4", @5, @"20"]
@see -sortBy
PROTIP: Internally `sort` attempts to sort all objects using `-compare:`
but if that fails, it calls `-description` on all objects and uses
`-sortBy`.
*/
- (NSArray *)sort;
/**
Sorts all objects using the return value of the given block as the
sorting criteria.
MKShape *rhombas = [MKShape new]; rhombas.title = @"rhombas";
MKShape *ellipse = [MKShape new]; ellipse.title = @"ellipse";
MKShape *hexagon = [MKShape new]; hexagon.title = @"hexagon";
id rv = @[rhombas, ellipse, hexagon].sortBy(^(id shape){
return [shape title];
});
// rv => @[ellipse, hexagon, rhombas]
id rv = @[rhombas, ellipse, hexagon].sortBy(@"title")
// rv => @[ellipse, hexagon, rhombas]
id rv = @[
@{@"name": @"frank", @"age": @32},
@{@"name": @"frank", @"age": @31},
@{@"name": @"bob", @"age": @54},
@{@"name": @"zane", @"age": @1},
@{@"name": @"frank", @"age": @12}
].sortBy(^(id o){
return @[o[@"name"], o[@"age"]];
});
// rv => @[
// @{@"name": @"bob", @"age": @54},
// @{@"name": @"frank", @"age": @12},
// @{@"name": @"frank", @"age": @31},
// @{@"name": @"frank", @"age": @32},
// @{@"name": @"zane", @"age": @1}
// ];
PROTIP: You will need you to implement a compare: method for any custom
objects you return.
PROTIP: Returning an array from your block will sort the receiver so
that the first object is the first sorting criteria, and the second
object is the second sorting criteria, et cetera.
*/
- (NSArray *(^)(id blockOrKey))sortBy;
@end