1. 程式人生 > >iOS之NSArray陣列排序

iOS之NSArray陣列排序

一、陣列遍歷

  除了常用的for和for-in遍歷外,系統還提供了三種列舉遍歷,對於大量的資料遍歷可以使用下列三個方法。

- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void
(NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); - (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6
), ios(4.0), watchos(2.0), tvos(9.0));

  定義一個數組,資料來源如下:Xcode輸出中文

2018-11-15 09:35:55.830699+0800 Test[2267:150185] ===(
    "name:往往0,age:0,sex:sex0",
    "name:往往1,age:1,sex:sex1",
    "name:往往7,age:7,sex:sex7",
    "name:往往2,age:2,sex:sex2",
    "name:往往3,age:3,sex:sex3",
    "name:往往4,age:4,sex:sex4"
)

  一、1 enumerateObjectsUsingBlock 陣列正常列舉

    [modelArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"11111index=%ld, obj==%@",idx,obj);
    }];

  效果:

2018-11-15 09:48:01.359039+0800 Test[2590:176418] 11111index=0, obj==name:往往0,age:0,sex:sex0
2018-11-15 09:48:01.359180+0800 Test[2590:176418] 11111index=1, obj==name:往往1,age:1,sex:sex1
2018-11-15 09:48:01.359298+0800 Test[2590:176418] 11111index=2, obj==name:往往7,age:7,sex:sex7
2018-11-15 09:48:01.359398+0800 Test[2590:176418] 11111index=3, obj==name:往往2,age:2,sex:sex2
2018-11-15 09:48:01.359491+0800 Test[2590:176418] 11111index=4, obj==name:往往3,age:3,sex:sex3
2018-11-15 09:48:01.359590+0800 Test[2590:176418] 11111index=5, obj==name:往往4,age:4,sex:sex4

  一、2 enumerateObjectsWithOptions 指定排序方式(此排序只是對陣列進行倒序列舉,並不是對數組裡面的資料進行排序處理)

  NSEnumerationOptions:列舉介紹

typedef NS_OPTIONS(NSUInteger, NSEnumerationOptions) {
    NSEnumerationConcurrent = (1UL << 0),//多執行緒來併發實現,並不保證按照順序執行
    NSEnumerationReverse = (1UL << 1),//倒序
};

  BOOL * _Nonnull stop: 指定條件停止列舉:*stop = YES(YES表示暫停)

    [modelArr enumerateObjectsWithOptions:NSEnumerationReverse
                               usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                   if (idx==4) {
                                       *stop = YES;
                                   }
                                   NSLog(@"2222index=%ld, obj==%@",idx,obj);
                               }];

  效果:倒序索引為4時暫停

2018-11-15 09:48:01.359790+0800 Test[2590:176418] 2222index=5, obj==name:往往4,age:4,sex:sex4
2018-11-15 09:48:01.359899+0800 Test[2590:176418] 2222index=4, obj==name:往往3,age:3,sex:sex3

  一、3 enumerateObjectsAtIndexes 指定原陣列範圍

    [modelArr enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]
                                options:NSEnumerationReverse
                             usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                 NSLog(@"33333index=%ld, obj==%@",idx,obj);
                             }];

  效果:原陣列範圍(1,3)進行列舉

2018-11-15 09:55:54.010262+0800 Test[2702:191253] 33333index=3, obj==name:往往2,age:2,sex:sex2
2018-11-15 09:55:54.010399+0800 Test[2702:191253] 33333index=2, obj==name:往往7,age:7,sex:sex7
2018-11-15 09:55:54.010636+0800 Test[2702:191253] 33333index=1, obj==name:往往1,age:1,sex:sex1

 

二、陣列中資料排序

  NSSortDescriptor:設定規則,第二個引數ascending(YES表示降序排列,NO表示升序排列)

  本例規則按照age:去陣列每條資料的age鍵,按照對應鍵的值進行排序;(如果數組裡封裝的是字典形如 @[@{},@{}] 的形式這種排序就不適用了)

  ascending:NO升序排列

  sortUsingDescriptors(可變陣列的分類)和sortedArrayUsingDescriptors(不可變陣列分類)方法效果相同。

    NSSortDescriptor *indexSD=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
//    NSMutableArray *temArr = [[modelArr sortedArrayUsingDescriptors:@[indexSD]] mutableCopy];
    [modelArr sortUsingDescriptors:@[indexSD]];
    NSLog(@"===%@",modelArr);

  對上列資料按照年齡age進行排序效果如圖:

2018-11-15 10:02:24.278353+0800 Test[2817:203857] ===(
    "name:往往0,age:0,sex:sex0",
    "name:往往1,age:1,sex:sex1",
    "name:往往7,age:7,sex:sex7",
    "name:往往2,age:2,sex:sex2",
    "name:往往3,age:3,sex:sex3",
    "name:往往4,age:4,sex:sex4"
)
2018-11-15 10:02:24.278669+0800 Test[2817:203857] ===(
    "name:往往7,age:7,sex:sex7",
    "name:往往4,age:4,sex:sex4",
    "name:往往3,age:3,sex:sex3",
    "name:往往2,age:2,sex:sex2",
    "name:往往1,age:1,sex:sex1",
    "name:往往0,age:0,sex:sex0"
)