美文网首页
Objective-C 数组排序

Objective-C 数组排序

作者: 小胡子杰克 | 来源:发表于2016-11-28 17:22 被阅读43次

先列出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
)

相关文章

  • Objective-C 数组排序

    先列出Foundation中提供的数组排序API 1.sortedArrayUsingFunction: cont...

  • iOS基本算法之二分法排序

    话不多说,上代码 Objective-C 实现 对以下数组的排序结果如下: Swift实现(swift2.0)

  • iOS中数组排序 对象属性排序

    在开发中,经常会用到排序功能,数组排序比较常见,但是在Objective-C中没有像swift一样的直接针对对象某...

  • iOS 各种排序

    数组排序 数组中字典排序 数组中字典按照某个value排序 排序方法

  • Java 数组的排序、逆序

    数组的排序、逆序测试数据 数组选择排序 数组冒泡排序 数组逆序

  • java 数组和list排序

    数组排序 其中有数组排序和数组对象排序 数组一些数字排序则直接用Arrays.sort()加数组就可以。数组对象则...

  • 基本算法

    冒泡排序、插入排序、选择排序、快速排序、二分查找(Objective-C实现)

  • 数组

    数组的遍历 数组是值类型 数组的排序 冒泡排序 多维数组

  • 2018-01-14

    php数组排序 sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据...

  • PHP排序算法

    排序算法 冒泡排序(数组排序) 快速排序(数组排序) 参考 http://www.cnblogs.com/enia...

网友评论

      本文标题:Objective-C 数组排序

      本文链接:https://www.haomeiwen.com/subject/wxsopttx.html