天天看點

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"
)      

轉載于:https://www.cnblogs.com/xianfeng-zhang/p/9961910.html

繼續閱讀