├── README.md ├── SortingForArray.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── ChenMan.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── ChenMan.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── SortingForArray.xcscheme │ └── xcschememanagement.plist └── SortingForArray └── main.m /README.md: -------------------------------------------------------------------------------- 1 | 2 | # iOS开发·必会的算法操作:字符串数组排序+模型对象数组排序 3 | 4 | 5 | 6 | # 前面的话 7 | 为了给字符串数组排序,除了用C/C++的基本办法,iOS开发者更应该学会利用苹果专门为**NSArray** 排序提供的**sortedArrayUsingComparator** 方法: 8 | 9 | ``` 10 | - (NSArray *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0); 11 | ``` 12 | 13 | 其中,需要设置一个**NSComparator** 参数,它是一个block,查看定义如下: 14 | ``` 15 | typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); 16 | ``` 17 | 这个block体返回的**NSComparisonResult** 是一个枚举类型,它的定义是: 18 | ``` 19 | typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}; 20 | ``` 21 | > 问题来了,怎么设置? 22 | 23 | - 为了设置这个**NSComparator** 参数的block体,你可以在设置其block体的时候,手动返回一个**NSComparisonResult** 枚举类型的某个具体值(**NSOrderedAscending**, **NSOrderedSame**, **NSOrderedDescending** 三选一): 24 | 25 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5b837c998e?w=1240&h=418&f=png&s=178143) 26 | 27 | - 如果数组里面是字符串,在设置其block体的时候,你也可以利用苹果专门为**NSString** 提供的字符串比较方法,获得一个**NSComparisonResult** 类型,将其自动返回。 28 | 29 | ``` 30 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(nullable id)locale; // locale arg used to be a dictionary pre-Leopard. We now accept NSLocale. Assumes the current locale if non-nil and non-NSLocale. nil continues to mean canonical compare, which doesn't depend on user's locale choice. 31 | ``` 32 | 33 | 34 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5b8339e10b?w=1240&h=205&f=png&s=125496) 35 | 36 | 37 | 38 | 这时候,就需要了解**NSStringCompareOptions** 的意思。但如果你搜索一下**NSStringCompareOptions** ,会发现很多文章中的翻译或者中文解释在误导,或者很难看清什么意思?例如下面这篇博客: 39 | 40 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5b84f50b00?w=1240&h=742&f=png&s=354395) 41 | 42 | 然后,相同的解释文案还以讹传讹的传开来了,例如你看下面这个博客: 43 | 44 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5d587e2c6f?w=1240&h=799&f=png&s=247510) 45 | 46 | 于是,笔者决定写此本文,好好展示他们的用途。 47 | 48 | 49 | # 1. 第一种:数组的字符串元素里面是基本数据类型 50 | --- 51 | ### 1.1 字符串数组排序示例 52 | ##### 1.1.1 实验代码 53 | - main.m 54 | ``` 55 | void handleSortingForIntStrArray(void){ 56 | NSArray *originalArray = @[@"00",@"0",@"00",@"01",@"10",@"21",@"12",@"11",@"22"]; 57 | //block比较方法,数组中可以是NSInteger,NSString(需要转换) 58 | NSComparator finderSort = ^(id string1,id string2){ 59 | if ([string1 integerValue] > [string2 integerValue]) { 60 | return (NSComparisonResult)NSOrderedDescending; 61 | }else if ([string1 integerValue] < [string2 integerValue]){ 62 | return (NSComparisonResult)NSOrderedAscending; 63 | }else{ 64 | return (NSComparisonResult)NSOrderedSame; 65 | } 66 | }; 67 | //数组排序: 68 | NSArray *resultArray = [originalArray sortedArrayUsingComparator:finderSort]; 69 | NSLog(@"第一种排序结果:%@",resultArray); 70 | } 71 | 72 | int main(int argc, const char * argv[]) { 73 | @autoreleasepool { 74 | // insert code here... 75 | NSLog(@"Results of handleSortingForIntArray()**********************"); 76 | handleSortingForIntStrArray(); 77 | } 78 | return 0; 79 | } 80 | ``` 81 | ##### 1.1.2 运行结果 82 | 83 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5b855576da?w=1170&h=408&f=png&s=49625) 84 | 85 | 86 | ##### 1.1.3 实验结论 87 | 88 | - 依据数组元素的数值大小返回升序数组 89 | 90 | ### 1.2 NSComparator与NSComparisonResult 91 | 上面的代码中用到了NSComparator与NSComparisonResult,在本文的“前面的话”中已经介绍过,这里重新列一下定义。 92 | ##### 1.2.1 NSComparator 93 | ``` 94 | typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); 95 | ``` 96 | ##### 1.2.2 NSComparisonResult 97 | ``` 98 | typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}; 99 | ``` 100 | 101 | # 2. 第二种:数组的字符串元素里面不是基本数据类型 102 | --- 103 | ### 2.1 示例:字符串数组排序 104 | ##### 2.1.1 实验代码 105 | - main.m 106 | ``` 107 | // 108 | // main.m 109 | // SortingForArray 110 | // 111 | // Created by ChenMan on 2017/12/20. 112 | // Copyright © 2017年 ChenMan. All rights reserved. 113 | // 114 | 115 | #import 116 | #import 117 | 118 | void handleSortingForStrArray(void){ 119 | NSArray *stringsArray = [NSArray arrayWithObjects: 120 | @"string b", 121 | @"string A", 122 | @"string a", 123 | @"string \uFF41", 124 | @"string a", 125 | @"string A", 126 | @"string c", 127 | @"string d0030", 128 | @"string d2", 129 | @"アいろはアイウエイウエ", 130 | @"アいろはアイウエイウエ", 131 | @"アいろはアイウエイウエ",nil]; 132 | 133 | NSLocale *currentLocale = [NSLocale currentLocale]; 134 | NSComparator finderSortBlock = ^(id string1,id string2) { 135 | 136 | NSRange string1Range =NSMakeRange(0, [string1 length]); 137 | return [string1 compare:string2 options:nil range:string1Range locale:currentLocale]; 138 | }; 139 | 140 | NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock]; 141 | NSLog(@"finderSortArray: %@", finderSortArray); 142 | 143 | } 144 | 145 | int main(int argc, const char * argv[]) { 146 | @autoreleasepool { 147 | // insert code here... 148 | NSLog(@"Results of handleSortingForStrArray()**********************"); 149 | handleSortingForStrArray(); 150 | } 151 | return 0; 152 | } 153 | ``` 154 | ##### 2.1.2 运行结果: 155 | 156 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5d5a4e7375?w=1200&h=484&f=png&s=94341) 157 | 158 | 159 | ##### 2.1.3 实验结论: 160 | 161 | 如上实验代码中,有这样一行代码: 162 | ``` 163 | return [string1 compare:string2 options:nil range:string1Range locale:currentLocale]; 164 | ``` 165 | 根据运行结果,可知如下结论: 166 | 167 | - 即使在`- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(nullable id)locale; `中将`(NSStringCompareOptions)`枚举类型的参数设置为`nil`,也可以运行。但一般不这么做,这里只是为了观察不指定该枚举参数时候系统的默认设置,并与本文接下来指定该枚举参数的排序结果对比。 168 | - 可以发现: 169 | - 默认同一字符的全角字符看做半角字符。不区分同一个字符(如日文的片假字)的半角与全角状态。相同元素,维持原序。 170 | - 默认区分字母大小写,同一个字符小写在前,大写在后。 171 | - 字母并非按unicode码的大小升序排列。例如,`全角a`的unicode为FF41,`半角a`的unicode为0061,`半角A`的unicode为0041,`半角b`的unicode为0062,但排序结果是 `全角a` = `半角a` < `半角A` < `半角b`。 172 | - 默认不识别含有数字字符的数值大小,0030虽然数学意义比2大,但是,仅从字符串的角度看,第一个字符0比2小,所以d0030排在d2前面。 173 | 174 | ##### 2.1.4 知识拓展: 175 | > 半角与全角字符 176 | - 全角占两个字节,半角占一个字节。通常我们碰到的英文字母、数字键、符号键这种ASCII码系统里面的字符大多数情况下是半角的。 177 | 178 | - 国内汉字输入法输入的汉字为全角,字母数字为半角,但是标点则默认为全角,可切换为半角(可以通过输入法工具条上的相应按钮来切换标点符号的全角半角状态)。 179 | 180 | - 日文里面的有汉字,也有片假字。这个片假字有两套编码,同一个片假字分别有半角和全角两种编码。例如:看起来像一样的片假字组成的句子,全角状态`ア`字符开头的为`アいろはアイウエイウエ`,半角状态`ア`字符开头的为`アいろはアイウエイウエ`。可以看到,明显同一个片假字的**全角状态** 比**半角状态** “胖”一圈。 181 | 182 | - 英文字母其实也有全角字母,例如小写的`a`,其半角形式的unicode码为0061,其全角形式的unicode码为FF41。可查阅[Unicode®字符百科](https://unicode-table.com/cn/)官网。 183 | 184 | 185 | ### 2.2 NSStringCompareOptions 186 | 187 | NSStringCompareOptions是一个枚举类型,并非一个类。打开NSStringCompareOptions的定义,可查看如下 188 | ``` 189 | typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) { 190 | NSCaseInsensitiveSearch = 1, 191 | NSLiteralSearch = 2, /* Exact character-by-character equivalence */ 192 | NSBackwardsSearch = 4, /* Search from end of source string */ 193 | NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */ 194 | NSNumericSearch = 64, /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */ 195 | NSDiacriticInsensitiveSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 128, /* If specified, ignores diacritics (o-umlaut == o) */ 196 | NSWidthInsensitiveSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 256, /* If specified, ignores width differences ('a' == UFF41) */ 197 | NSForcedOrderingSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */ 198 | NSRegularExpressionSearch API_AVAILABLE(macos(10.7), ios(3.2), watchos(2.0), tvos(9.0)) = 1024 /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */ 199 | }; 200 | ``` 201 | 202 | ##### 2.2.1 NSNumericSearch 203 | 204 | > 官方解释:Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find 205 | 206 | - 假设,将上例中的部分代码修改为 207 | ``` 208 | void handleSortingForStrArray(void){ 209 | NSArray *stringsArray = [NSArray arrayWithObjects: 210 | @"string b", 211 | @"string A", 212 | @"string a", 213 | @"string \uFF41", 214 | @"string a", 215 | @"string A", 216 | @"string c", 217 | @"string d0030", 218 | @"string d2", 219 | @"アいろはアイウエイウエ", 220 | @"アいろはアイウエイウエ", 221 | @"アいろはアイウエイウエ",nil]; 222 | NSStringCompareOptions comparisonOptions = NSNumericSearch; 223 | NSLocale *currentLocale = [NSLocale currentLocale]; 224 | NSComparator finderSortBlock = ^(id string1,id string2) { 225 | 226 | NSRange string1Range =NSMakeRange(0, [string1 length]); 227 | return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale]; 228 | }; 229 | 230 | NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock]; 231 | NSLog(@"finderSortArray: %@", finderSortArray); 232 | } 233 | ``` 234 | - 运行结果 235 | 236 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5b8be0c123?w=1156&h=530&f=png&s=98132) 237 | 238 | 239 | - 结论 240 | `NSStringCompareOptions`指定为`NSNumericSearch`,当字符串中含有数字时,从数值大小的角度按升序排序。 241 | 242 | ##### 2.2.2 NSCaseInsensitiveSearch 243 | 244 | > 官方解释:无。英文字面解释:不区分字母大小写。 245 | 246 | - 假设,将上例中的部分代码修改为 247 | ``` 248 | NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch; 249 | ``` 250 | - 运行结果 251 | 252 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5b996ca3aa?w=1184&h=488&f=png&s=93423) 253 | 254 | 255 | - 结论 256 | `NSStringCompareOptions`指定为`NSCaseInsensitiveSearch`,不区分同一个字母的大小写状态,如`a`与`A`看做相同元素,若其它条件也一致则保持原序。 257 | 258 | ##### 2.2.3 NSLiteralSearch 259 | 260 | > 官方解释:Exact character-by-character equivalence 261 | 262 | - 假设,将上例中的部分代码修改为 263 | ``` 264 | NSStringCompareOptions comparisonOptions = NSLiteralSearch; 265 | ``` 266 | 267 | - 运行结果 268 | 269 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5baebfc221?w=1122&h=522&f=png&s=95444) 270 | 271 | - 结论 272 | - ***区分*** 同一个字符(如日文的片假字)的半角与全角状态,同一片假字的全角状态小于半角状态。 273 | - 其它规则,继续按系统默认排序规则排序,包括**默认区分** 字母大小写,以及其它默认排序规则。 274 | - 按照官方英文说明,这个规则是指区分每个字符的等效状态。只要unicode不同的字符,就不认可他们“等效”,即使他们的语言上的含义相同。 275 | 276 | 277 | - 题外话 278 | - 所以,有的文献说**NSLiteralSearch** 是区分大小写是误导,系统本就**默认区分** 字母大小写,这些人以为苹果公司提供这个功能来画蛇添足干嘛?而且可以看看官方英文说明,也不是这个意思。只有指定**不区分** 字母大小写的**NSCaseInsensitiveSearch**,要么不写,即默认**区分**。 279 | 280 | 281 | 282 | ##### 2.2.4 NSWidthInsensitiveSearch 283 | > 官方解释:If specified, ignores width differences ('a' == UFF41) 284 | - 假设,将上例中的部分代码修改为 285 | ``` 286 | NSStringCompareOptions comparisonOptions = NSWidthInsensitiveSearch; 287 | ``` 288 | 289 | - 运行结果 290 | 291 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5baf52dd62?w=1206&h=470&f=png&s=95331) 292 | 293 | - 结论 294 | - **不区分** 同一个字符(如日文的片假字)的半角与全角状态,同一片假字的全角状态等于半角状态。 295 | - 其它规则,继续按系统默认排序规则排序,包括**默认区分** 字母大小写,以及其它默认排序规则。 296 | - 同时指定两个时,**NSWidthInsensitiveSearch** 比**NSLiteralSearch** 的优先级高,综合起来的结果是**不区分** 半角全角。 297 | - 官方英文说明中的`UFF41`是指`全角a`,`'a'` 是指`半角a`,如果指定**NSWidthInsensitiveSearch**,则不区分字符的全角半角,即使你同时指定了**NSLiteralSearch**。 298 | 299 | 即,当有如下代码 300 | ``` 301 | NSStringCompareOptions comparisonOptions = NSWidthInsensitiveSearch | NSLiteralSearch; 302 | ``` 303 | 其作用相当于没有NSLiteralSearch的代码 304 | ``` 305 | NSStringCompareOptions comparisonOptions = NSWidthInsensitiveSearch; 306 | ``` 307 | 308 | ##### 2.2.5 NSForcedOrderingSearch 309 | > 官方解释:If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) 310 | 311 | - 假设,将上例中的部分代码修改为 312 | ``` 313 | NSStringCompareOptions comparisonOptions = NSForcedOrderingSearch; 314 | ``` 315 | - 运行结果 316 | 317 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5bb3b8663b?w=1208&h=492&f=png&s=96670) 318 | 319 | - 结论 320 | - 不存在字符等不等效相不相等的概念了,只要unicode不一样的字符,必须区分,必须返回一个谁大谁小的结果(NSOrderedAscending or NSOrderedDescending)。 321 | - 从英文说明也可以看出,**NSForcedOrderingSearch** 的优先级最高,即如果你同时指定了其它有可能作用冲突的枚举类型,也以**NSForcedOrderingSearch** 的作用为准。 322 | 323 | ##### 2.2.6 综合应用 324 | - 一个比较多的应用示例是,区分字母大小写,区分数值大小,区分半角全角,并强制性指定区分unicode不一样的字符。综合这些条件,写起来就是: 325 | ``` 326 | NSStringCompareOptions comparisonOptions = NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch; 327 | ``` 328 | - 运行结果 329 | 330 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5bb53864bb?w=1184&h=476&f=png&s=94997) 331 | 332 | ##### 2.2.7 误导用法 333 | - 我看过有很多其它博客用了这样的误导示例: 334 | ``` 335 | NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch; 336 | ``` 337 | 这里面,NSCaseInsensitiveSearch是为了不区分大小写字母,但是后面再加个NSForcedOrderingSearch想强制区分字符又是怎么回事?虽然,这样写并不会报错,运行效果跟上面的综合示例一摸一样。但这样误导的想法是个逻辑矛盾。不信,你看看它运行的结果: 338 | 339 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5bb65dc318?w=1174&h=462&f=png&s=91486) 340 | 341 | 342 | # 3. 数组里面是类的对象 343 | --- 344 | 需求:假设我们根据后台返回的JSON字典数组用MJExtension转换成模型数组,现在我们需要根据ID或者Age对模型数组进行排序。 345 | 346 | - Pesson.m 347 | ``` 348 | #import 349 | 350 | @interface Person : NSObject 351 | @property (nonatomic,copy) NSString *ID; 352 | @property (nonatomic,copy) NSString *name; 353 | @property (nonatomic,assign) int age; 354 | @end 355 | ``` 356 | - 根据int类型的属性对模型数组进行排序 357 | ``` 358 | NSArray *sortArrayByAgeInt = [self.dataArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 359 | 360 | Person *pModel1 = obj1; 361 | Person *pModel2 = obj2; 362 | 363 | if (pModel1.age > pModel2.age) { 364 | return NSOrderedDescending;//降序 365 | }else if (pModel1.name < pModel2.name){ 366 | return NSOrderedAscending;//升序 367 | }else { 368 | return NSOrderedSame;//相等 369 | } 370 | 371 | }]; 372 | ``` 373 | - 根据str类型的属性对模型数组进行排序 374 | ``` 375 | NSArray *sortArrayByIDStr = [self.dataArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 376 | 377 | Person *pModel1 = obj1; 378 | Person *pModel2 = obj2; 379 | 380 | if ([pModel1.ID intValue]> [pModel2.ID intValue]) { 381 | return NSOrderedDescending;//降序 382 | }else if (pModel1.name < pModel2.name){ 383 | return NSOrderedAscending;//升序 384 | }else { 385 | return NSOrderedSame;//相等 386 | } 387 | 388 | }]; 389 | ``` 390 | # 4. 花样玩法:例题 391 | --- 392 | 在OC的高级用法中,经常需要查看系统类或者某个自定义类中的私有属性以及私有成员变量,并通过KVC的办法强制修改这些私有成员变量的值,以取代系统或者自定义类中的默认设置。所以,如果你懒得创建一些假数据的数组,可以想到运用运行时的办法获取成员变量的数组,并进行排序操作训练。 393 | 394 | > **题1**. 请取出`NSString`类的全部**公有** **属性** 并存放到一个数组,并利用`NSArray`的`sortedArrayUsingComparator`的方法给这个数组进行升序排序操作。要求:排序过程中需要区分字符全角半角状态,其它可按系统默认条件。 395 | 396 | - 参考代码: 397 | main.m 398 | ``` 399 | void handlePrintingOfProperties(void){ 400 | unsigned int count;// 记录属性个数 401 | objc_property_t *properties = class_copyPropertyList([NSString class], &count); 402 | // 生成一个属性名称组成的数组 403 | NSMutableArray *propertyNameArray = [NSMutableArray array]; 404 | for (int i = 0; i < count; i++) { 405 | // An opaque type that represents an Objective-C declared property. 406 | // objc_property_t 属性类型 407 | objc_property_t property = properties[i]; 408 | // 获取属性的名称 C语言字符串 409 | const char *cName = property_getName(property); 410 | // 转换为Objective C 字符串 411 | NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding]; 412 | [propertyNameArray addObject:name]; 413 | } 414 | NSLog(@"排序前的属性列表 = %@",propertyNameArray); 415 | 416 | NSComparator cmptr = ^(NSString *obj1, NSString *obj2){ 417 | return [obj1 compare:obj2 options:NSLiteralSearch]; 418 | }; 419 | NSArray *afterSort = [propertyNameArray sortedArrayUsingComparator:cmptr]; 420 | NSLog(@"排序后的属性列表 = %@",afterSort); 421 | 422 | //C语言中,用完copy,create的东西之后,最好释放 423 | free(properties); 424 | } 425 | 426 | int main(int argc, const char * argv[]) { 427 | @autoreleasepool { 428 | NSLog(@"handlePrintingOfProperties()**********************"); 429 | handlePrintingOfProperties(); 430 | } 431 | return 0; 432 | } 433 | ``` 434 | 435 | - 运行结果 436 | 437 | ![image.png](https://github.com/cimain/MediaResourcesForNotes/blob/master/SortingForArray/s1.png?raw=true) 438 | 439 | 440 | > **题2**. 请取出`NSURL`类中包括**私有** 在内的**全部** **成员变量**,并存放到一个数组,并利用`NSArray`的`sortedArrayUsingComparator`的方法给这个数组进行升序排序操作。要求:排序过程中需要区分字符全角半角状态,其它可按系统默认条件。 441 | 442 | - 参考代码: 443 | ``` 444 | void handlePrintingOfIvars(void){ 445 | unsigned int count;// 记录属性个数 446 | Ivar *properties = class_copyIvarList([NSURL class], &count); 447 | // 生成一个属性名称组成的数组 448 | NSMutableArray *propertyNameArray = [NSMutableArray array]; 449 | for (int i = 0; i < count; i++) { 450 | // An opaque type that represents an Objective-C declared property. 451 | // objc_property_t 属性类型 452 | Ivar property = properties[i]; 453 | // 获取属性的名称 C语言字符串 454 | const char *cName = ivar_getName(property); 455 | // 转换为Objective C 字符串 456 | NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding]; 457 | [propertyNameArray addObject:name]; 458 | } 459 | NSLog(@"排序前的成员变量列表 = %@",propertyNameArray); 460 | 461 | NSComparator cmptr = ^(NSString *obj1, NSString *obj2){ 462 | return [obj1 compare:obj2 options:NSLiteralSearch]; 463 | }; 464 | NSArray *afterSort = [propertyNameArray sortedArrayUsingComparator:cmptr]; 465 | NSLog(@"排序后的成员变量列表 = %@",afterSort); 466 | 467 | //C语言中,用完copy,create的东西之后,最好释放 468 | free(properties); 469 | } 470 | 471 | int main(int argc, const char * argv[]) { 472 | @autoreleasepool { 473 | NSLog(@"handlePrintingOfIvars()**********************"); 474 | handlePrintingOfIvars(); 475 | } 476 | return 0; 477 | } 478 | ``` 479 | 480 | - 运行结果 481 | 482 | ![image.png](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5be3766c71?w=1182&h=502&f=png&s=82314) 483 | 484 | 485 | 486 | # 5. 附录:本实验中创建工程说明 487 | --- 488 | 任何能在计算机上执行的项目称之为**程序**,其中,有图形化用户界面的程序称之为**应用** ,没有图形界面的程序可以是**守护进程** ,还有一种称之为**命令行工具**。本文这里关注的是算法和数据结果,不关注图形界面,所以新建一个命令行工具即可。创建方法:新建一个macOS工程,选择Command Line Tool类型,点击下一步配置工程信息即可。 489 | 490 | ![创建一个命令行工具](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5be4e47d99?w=1240&h=952&f=png&s=192239) 491 | 492 | ![工程创建成功](https://user-gold-cdn.xitu.io/2018/4/12/162b7d5be78cfc44?w=1240&h=804&f=png&s=367110) 493 | 494 | -------------------------------------------------------------------------------- /SortingForArray.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F7FD85F71FEA101000BAC89A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F7FD85F61FEA101000BAC89A /* main.m */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | F7FD85F11FEA101000BAC89A /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = /usr/share/man/man1/; 18 | dstSubfolderSpec = 0; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 1; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | F7FD85F31FEA101000BAC89A /* SortingForArray */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SortingForArray; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | F7FD85F61FEA101000BAC89A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | F7FD85F01FEA101000BAC89A /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | F7FD85EA1FEA100F00BAC89A = { 42 | isa = PBXGroup; 43 | children = ( 44 | F7FD85F51FEA101000BAC89A /* SortingForArray */, 45 | F7FD85F41FEA101000BAC89A /* Products */, 46 | ); 47 | sourceTree = ""; 48 | }; 49 | F7FD85F41FEA101000BAC89A /* Products */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | F7FD85F31FEA101000BAC89A /* SortingForArray */, 53 | ); 54 | name = Products; 55 | sourceTree = ""; 56 | }; 57 | F7FD85F51FEA101000BAC89A /* SortingForArray */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | F7FD85F61FEA101000BAC89A /* main.m */, 61 | ); 62 | path = SortingForArray; 63 | sourceTree = ""; 64 | }; 65 | /* End PBXGroup section */ 66 | 67 | /* Begin PBXNativeTarget section */ 68 | F7FD85F21FEA101000BAC89A /* SortingForArray */ = { 69 | isa = PBXNativeTarget; 70 | buildConfigurationList = F7FD85FA1FEA101000BAC89A /* Build configuration list for PBXNativeTarget "SortingForArray" */; 71 | buildPhases = ( 72 | F7FD85EF1FEA101000BAC89A /* Sources */, 73 | F7FD85F01FEA101000BAC89A /* Frameworks */, 74 | F7FD85F11FEA101000BAC89A /* CopyFiles */, 75 | ); 76 | buildRules = ( 77 | ); 78 | dependencies = ( 79 | ); 80 | name = SortingForArray; 81 | productName = SortingForArray; 82 | productReference = F7FD85F31FEA101000BAC89A /* SortingForArray */; 83 | productType = "com.apple.product-type.tool"; 84 | }; 85 | /* End PBXNativeTarget section */ 86 | 87 | /* Begin PBXProject section */ 88 | F7FD85EB1FEA101000BAC89A /* Project object */ = { 89 | isa = PBXProject; 90 | attributes = { 91 | LastUpgradeCheck = 0830; 92 | ORGANIZATIONNAME = ChenMan; 93 | TargetAttributes = { 94 | F7FD85F21FEA101000BAC89A = { 95 | CreatedOnToolsVersion = 8.3.2; 96 | DevelopmentTeam = G9A57BVG7J; 97 | ProvisioningStyle = Automatic; 98 | }; 99 | }; 100 | }; 101 | buildConfigurationList = F7FD85EE1FEA101000BAC89A /* Build configuration list for PBXProject "SortingForArray" */; 102 | compatibilityVersion = "Xcode 3.2"; 103 | developmentRegion = English; 104 | hasScannedForEncodings = 0; 105 | knownRegions = ( 106 | en, 107 | ); 108 | mainGroup = F7FD85EA1FEA100F00BAC89A; 109 | productRefGroup = F7FD85F41FEA101000BAC89A /* Products */; 110 | projectDirPath = ""; 111 | projectRoot = ""; 112 | targets = ( 113 | F7FD85F21FEA101000BAC89A /* SortingForArray */, 114 | ); 115 | }; 116 | /* End PBXProject section */ 117 | 118 | /* Begin PBXSourcesBuildPhase section */ 119 | F7FD85EF1FEA101000BAC89A /* Sources */ = { 120 | isa = PBXSourcesBuildPhase; 121 | buildActionMask = 2147483647; 122 | files = ( 123 | F7FD85F71FEA101000BAC89A /* main.m in Sources */, 124 | ); 125 | runOnlyForDeploymentPostprocessing = 0; 126 | }; 127 | /* End PBXSourcesBuildPhase section */ 128 | 129 | /* Begin XCBuildConfiguration section */ 130 | F7FD85F81FEA101000BAC89A /* Debug */ = { 131 | isa = XCBuildConfiguration; 132 | buildSettings = { 133 | ALWAYS_SEARCH_USER_PATHS = NO; 134 | CLANG_ANALYZER_NONNULL = YES; 135 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 136 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 137 | CLANG_CXX_LIBRARY = "libc++"; 138 | CLANG_ENABLE_MODULES = YES; 139 | CLANG_ENABLE_OBJC_ARC = YES; 140 | CLANG_WARN_BOOL_CONVERSION = YES; 141 | CLANG_WARN_CONSTANT_CONVERSION = YES; 142 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 143 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 144 | CLANG_WARN_EMPTY_BODY = YES; 145 | CLANG_WARN_ENUM_CONVERSION = YES; 146 | CLANG_WARN_INFINITE_RECURSION = YES; 147 | CLANG_WARN_INT_CONVERSION = YES; 148 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 149 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 150 | CLANG_WARN_UNREACHABLE_CODE = YES; 151 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 152 | CODE_SIGN_IDENTITY = "-"; 153 | COPY_PHASE_STRIP = NO; 154 | DEBUG_INFORMATION_FORMAT = dwarf; 155 | ENABLE_STRICT_OBJC_MSGSEND = YES; 156 | ENABLE_TESTABILITY = YES; 157 | GCC_C_LANGUAGE_STANDARD = gnu99; 158 | GCC_DYNAMIC_NO_PIC = NO; 159 | GCC_NO_COMMON_BLOCKS = YES; 160 | GCC_OPTIMIZATION_LEVEL = 0; 161 | GCC_PREPROCESSOR_DEFINITIONS = ( 162 | "DEBUG=1", 163 | "$(inherited)", 164 | ); 165 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 166 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 167 | GCC_WARN_UNDECLARED_SELECTOR = YES; 168 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 169 | GCC_WARN_UNUSED_FUNCTION = YES; 170 | GCC_WARN_UNUSED_VARIABLE = YES; 171 | MACOSX_DEPLOYMENT_TARGET = 10.13; 172 | MTL_ENABLE_DEBUG_INFO = YES; 173 | ONLY_ACTIVE_ARCH = YES; 174 | SDKROOT = macosx; 175 | }; 176 | name = Debug; 177 | }; 178 | F7FD85F91FEA101000BAC89A /* Release */ = { 179 | isa = XCBuildConfiguration; 180 | buildSettings = { 181 | ALWAYS_SEARCH_USER_PATHS = NO; 182 | CLANG_ANALYZER_NONNULL = YES; 183 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 184 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 185 | CLANG_CXX_LIBRARY = "libc++"; 186 | CLANG_ENABLE_MODULES = YES; 187 | CLANG_ENABLE_OBJC_ARC = YES; 188 | CLANG_WARN_BOOL_CONVERSION = YES; 189 | CLANG_WARN_CONSTANT_CONVERSION = YES; 190 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 191 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 192 | CLANG_WARN_EMPTY_BODY = YES; 193 | CLANG_WARN_ENUM_CONVERSION = YES; 194 | CLANG_WARN_INFINITE_RECURSION = YES; 195 | CLANG_WARN_INT_CONVERSION = YES; 196 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNREACHABLE_CODE = YES; 199 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 200 | CODE_SIGN_IDENTITY = "-"; 201 | COPY_PHASE_STRIP = NO; 202 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 203 | ENABLE_NS_ASSERTIONS = NO; 204 | ENABLE_STRICT_OBJC_MSGSEND = YES; 205 | GCC_C_LANGUAGE_STANDARD = gnu99; 206 | GCC_NO_COMMON_BLOCKS = YES; 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | MACOSX_DEPLOYMENT_TARGET = 10.13; 214 | MTL_ENABLE_DEBUG_INFO = NO; 215 | SDKROOT = macosx; 216 | }; 217 | name = Release; 218 | }; 219 | F7FD85FB1FEA101000BAC89A /* Debug */ = { 220 | isa = XCBuildConfiguration; 221 | buildSettings = { 222 | DEVELOPMENT_TEAM = G9A57BVG7J; 223 | PRODUCT_NAME = "$(TARGET_NAME)"; 224 | }; 225 | name = Debug; 226 | }; 227 | F7FD85FC1FEA101000BAC89A /* Release */ = { 228 | isa = XCBuildConfiguration; 229 | buildSettings = { 230 | DEVELOPMENT_TEAM = G9A57BVG7J; 231 | PRODUCT_NAME = "$(TARGET_NAME)"; 232 | }; 233 | name = Release; 234 | }; 235 | /* End XCBuildConfiguration section */ 236 | 237 | /* Begin XCConfigurationList section */ 238 | F7FD85EE1FEA101000BAC89A /* Build configuration list for PBXProject "SortingForArray" */ = { 239 | isa = XCConfigurationList; 240 | buildConfigurations = ( 241 | F7FD85F81FEA101000BAC89A /* Debug */, 242 | F7FD85F91FEA101000BAC89A /* Release */, 243 | ); 244 | defaultConfigurationIsVisible = 0; 245 | defaultConfigurationName = Release; 246 | }; 247 | F7FD85FA1FEA101000BAC89A /* Build configuration list for PBXNativeTarget "SortingForArray" */ = { 248 | isa = XCConfigurationList; 249 | buildConfigurations = ( 250 | F7FD85FB1FEA101000BAC89A /* Debug */, 251 | F7FD85FC1FEA101000BAC89A /* Release */, 252 | ); 253 | defaultConfigurationIsVisible = 0; 254 | }; 255 | /* End XCConfigurationList section */ 256 | }; 257 | rootObject = F7FD85EB1FEA101000BAC89A /* Project object */; 258 | } 259 | -------------------------------------------------------------------------------- /SortingForArray.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SortingForArray.xcodeproj/project.xcworkspace/xcuserdata/ChenMan.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cimain/SortingForArray/df7ce6f12c4a22f1a925e4d90a6d7d8eb99fb151/SortingForArray.xcodeproj/project.xcworkspace/xcuserdata/ChenMan.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SortingForArray.xcodeproj/xcuserdata/ChenMan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /SortingForArray.xcodeproj/xcuserdata/ChenMan.xcuserdatad/xcschemes/SortingForArray.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /SortingForArray.xcodeproj/xcuserdata/ChenMan.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SortingForArray.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | F7FD85F21FEA101000BAC89A 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /SortingForArray/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SortingForArray 4 | // 5 | // Created by ChenMan on 2017/12/20. 6 | // Copyright © 2017年 ChenMan. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | 14 | void handlePrintingOfProperties(void){ 15 | unsigned int count;// 记录属性个数 16 | objc_property_t *properties = class_copyPropertyList([NSString class], &count); 17 | // 生成一个属性名称组成的数组 18 | NSMutableArray *propertyNameArray = [NSMutableArray array]; 19 | for (int i = 0; i < count; i++) { 20 | // An opaque type that represents an Objective-C declared property. 21 | // objc_property_t 属性类型 22 | objc_property_t property = properties[i]; 23 | // 获取属性的名称 C语言字符串 24 | const char *cName = property_getName(property); 25 | // 转换为Objective C 字符串 26 | NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding]; 27 | [propertyNameArray addObject:name]; 28 | } 29 | NSLog(@"排序前的属性列表 = %@",propertyNameArray); 30 | 31 | NSComparator cmptr = ^(NSString *obj1, NSString *obj2){ 32 | return [obj1 compare:obj2 options:NSLiteralSearch]; 33 | }; 34 | NSArray *afterSort = [propertyNameArray sortedArrayUsingComparator:cmptr]; 35 | NSLog(@"排序后的属性列表 = %@",afterSort); 36 | 37 | //C语言中,用完copy,create的东西之后,最好释放 38 | free(properties); 39 | } 40 | 41 | void handlePrintingOfIvars(void){ 42 | unsigned int count;// 记录属性个数 43 | Ivar *properties = class_copyIvarList([NSURL class], &count); 44 | // 生成一个属性名称组成的数组 45 | NSMutableArray *propertyNameArray = [NSMutableArray array]; 46 | for (int i = 0; i < count; i++) { 47 | // An opaque type that represents an Objective-C declared property. 48 | // objc_property_t 属性类型 49 | Ivar property = properties[i]; 50 | // 获取属性的名称 C语言字符串 51 | const char *cName = ivar_getName(property); 52 | // 转换为Objective C 字符串 53 | NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding]; 54 | [propertyNameArray addObject:name]; 55 | } 56 | NSLog(@"排序前的成员变量列表 = %@",propertyNameArray); 57 | 58 | NSComparator cmptr = ^(NSString *obj1, NSString *obj2){ 59 | return [obj1 compare:obj2 options:NSLiteralSearch]; 60 | }; 61 | NSArray *afterSort = [propertyNameArray sortedArrayUsingComparator:cmptr]; 62 | NSLog(@"排序后的成员变量列表 = %@",afterSort); 63 | 64 | //C语言中,用完copy,create的东西之后,最好释放 65 | free(properties); 66 | } 67 | 68 | void handleSortingForStrArray(void){ 69 | // NSArray *stringsArray = [NSArray arrayWithObjects: 70 | // @"string 10", 71 | // @"string 1", 72 | // @"string AAA1", 73 | // @"string aaa1", 74 | // @"string AAA2", 75 | // @"string 120", 76 | // @"string 1100", 77 | // @"string 02000", 78 | // nil]; 79 | NSArray *stringsArray = [NSArray arrayWithObjects: 80 | @"string b", 81 | @"string A", 82 | @"string a", 83 | @"string \uFF41", 84 | @"string a", 85 | @"string A", 86 | @"string c", 87 | @"string d0030", 88 | @"string d2", 89 | @"アいろはアイウエイウエ", 90 | @"アいろはアイウエイウエ", 91 | @"アいろはアイウエイウエ",nil]; 92 | 93 | NSStringCompareOptions comparisonOptions = NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch; 94 | 95 | NSLocale *currentLocale = [NSLocale currentLocale]; 96 | 97 | NSComparator finderSortBlock = ^(id string1,id string2) { 98 | 99 | NSRange string1Range =NSMakeRange(0, [string1 length]); 100 | return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale]; 101 | }; 102 | 103 | NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock]; 104 | NSLog(@"finderSortArray: %@", finderSortArray); 105 | 106 | } 107 | 108 | //void handleSortingForIntArray(void){ 109 | // NSArray *originalArray = @[@(00),@0,@00,@01,@10,@21,@12,@11,@22]; 110 | // //block比较方法,数组中可以是NSInteger,NSString(需要转换) 111 | // NSComparator finderSort = ^(id string1,id string2){ 112 | // if ([string1 integerValue] > [string2 integerValue]) { 113 | // return (NSComparisonResult)NSOrderedDescending; 114 | // }else if ([string1 integerValue] < [string2 integerValue]){ 115 | // return (NSComparisonResult)NSOrderedAscending; 116 | // }else{ 117 | // return (NSComparisonResult)NSOrderedSame; 118 | // } 119 | // }; 120 | // //数组排序: 121 | // NSArray *resultArray = [originalArray sortedArrayUsingComparator:finderSort]; 122 | // NSLog(@"Int数组排序结果:%@",resultArray); 123 | //} 124 | 125 | void handleSortingForIntStrArray(void){ 126 | NSArray *originalArray = @[@"00",@"0",@"00",@"01",@"10",@"21",@"12",@"11",@"22"]; 127 | //block比较方法,数组中可以是NSInteger,NSString(需要转换) 128 | NSComparator finderSort = ^(id string1,id string2){ 129 | if ([string1 integerValue] > [string2 integerValue]) { 130 | return (NSComparisonResult)NSOrderedDescending; 131 | }else if ([string1 integerValue] < [string2 integerValue]){ 132 | return (NSComparisonResult)NSOrderedAscending; 133 | }else{ 134 | return (NSComparisonResult)NSOrderedSame; 135 | } 136 | }; 137 | //数组排序: 138 | NSArray *resultArray = [originalArray sortedArrayUsingComparator:finderSort]; 139 | NSLog(@"第一种排序结果:%@",resultArray); 140 | } 141 | 142 | int main(int argc, const char * argv[]) { 143 | @autoreleasepool { 144 | // insert code here... 145 | // NSLog(@"Results of handleSortingForStrArray()**********************"); 146 | // handleSortingForStrArray(); 147 | // NSLog(@"Results of handleSortingForIntArray()**********************"); 148 | // handleSortingForIntStrArray(); 149 | // NSLog(@"handlePrintingOfProperties()**********************"); 150 | // handlePrintingOfProperties(); 151 | NSLog(@"handlePrintingOfIvars()**********************"); 152 | handlePrintingOfIvars(); 153 | } 154 | return 0; 155 | } 156 | 157 | --------------------------------------------------------------------------------