先列出Foundation中提供的数组排序API
@property (readonly, copy) NSData *sortedArrayHint;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors; // returns a new array by sorting the objects of the receiver
1.sortedArrayUsingFunction: context:
先看下官方文档的介绍:
Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.
The new array contains references to the receiving array’s elements, not copies of them.
方法会返回一个新的数组容器,这个数组中的元素的地址与元素地址相同
使用方法
1.1 自己定义的排序算法:
NSInteger sortFunction(NSString *obj1 , NSString *obj2, void * content) {
if ([obj1 integerValue] < [obj2 integerValue]) {
return NSOrderedAscending;
} else if([obj1 integerValue] > [obj2 integerValue]) {
return NSOrderedDescending;
}
return NSOrderedSame;
}
1.2 调用(不会改变原素组originArr):
NSArray *originArr = @[@"1", @"2", @"4", @"5", @"3"];
NSArray *functionArray = [originArr sortedArrayUsingFunction:sortFunction context:NULL];
NSLog(@"sortedArrayUsingFunction %@", functionArray);
NSLog(@"origin %@", originArr);
2016-11-28 14:59:24.207 ArraySortDemo[5006:138036] sortedArrayUsingFunction (
1,
2,
3,
4,
5
)
2016-11-28 14:59:24.207 ArraySortDemo[5006:138036]
origin (
1,
2,
4,
5,
3
)
2.sortedArrayUsingFunction: context: hint:
This method is similar to sortedArrayUsingFunction:context:, except that it uses the supplied hint to speed the sorting process. When you know the array is nearly sorted, this method is faster than sortedArrayUsingFunction:context:. If you sorted a large array (N entries) once, and you don’t change it much (P additions and deletions, where P is much smaller than N), then you can reuse the work you did in the original sort by conceptually doing a merge sort between the N “old” items and the P “new” items.
To obtain an appropriate hint, use sortedArrayHint. You should obtain this hint when the original array has been sorted, and keep hold of it until you need it, after the array has been modified. The hint is computed by sortedArrayHint in O(N) (where N is the number of items). This assumes that items in the array implement a -hash method. Given a suitable hint, and assuming that the hash function is a “good” hash function, -sortedArrayUsingFunction:context:hint: sorts the array in O(PLOG(P)+N) where P is the number of adds or deletes. This is an improvement over the un-hinted sort, O(NLOG(N)), when P is small.
这个方法的使用与sortedArrayUsingFunction: context: hint:
相同,根据官方文档介绍,这个方法中多出来的hint这个参数是用于在之后的排序中复用的,在下一次的排序时对数组的内容修改不大的时候,使用此方法可以降低此次排序算法复杂度,特别是在数组的体积非常庞大的时候显得更有必要
3.sortedArrayUsingSelector:
对参数SEL对象的要求:
A selector that identifies the method to use to compare two elements at a time. The method should return NSOrderedAscending if the receiving array is smaller than the argument, NSOrderedDescending if the receiving array is larger than the argument, and NSOrderedSame if they are equal.
example:
NSArray *originArr = @[@"1", @"2", @"4", @"5", @"3"];
NSArray *selectArr = [originArr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"sortedArrayUsingSelector %@", selectArr);
NSLog(@"origin %@", originArr);
2016-11-28 14:59:24.207 ArraySortDemo[5006:138036] sortedArrayUsingSelector (
1,
2,
3,
4,
5
)
2016-11-28 14:59:24.207 ArraySortDemo[5006:138036] origin (
1,
2,
4,
5,
3
)