NSArray*array=@[@"TimCook",@"JonyIve",@"SteveJobs",@"RobertBrunne",@"Philip Schiller"]; NSArray* sortArr = [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",sortArr); NSComparator sortBlock = ^(id obj1,id obj2) { return [obj1 compare:obj2]; }; NSArray* sortArr2 = [array sortedArrayUsingComparator:sortBlock]; NSLog(@"%@",sortArr2);
作业2
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //1.以下是身份证号,输出身份证人的性别,年龄 NSString *idCard=@"210423198809040427"; NSString* str =[NSString stringWithFormat:@"%@",idCard]; NSRange range = NSMakeRange(16, 1); NSRange range1 = NSMakeRange(6, 4); NSString* str1 = [str substringWithRange:range]; NSString* str2 =[str substringWithRange:range1]; int a =[str1 intValue]; int b =[str2 intValue]; if(a%2==0) { NSLog(@"性别女 年龄%d",2016-b); } else { NSLog(@"性别男 年龄%d",2016-b); } //身份证倒数#2位是偶数则为女生 //2.将字典的key从Z->A排序,按排序后的key的顺序,输出value,将value按字符串输出 NSDictionary *dict=@{@"R":@"e", @"T":@"e", @"D":@"b", @"S":@"u", @"K":@"a", @"A":@"s", @"O":@" ", @"N":@"p", @"B":@"b", @"J":@"u", @"F":@" ", @"U":@"t", @"H":@"l", @"E":@"j", @"Z":@"s", }; NSArray* array = [dict allKeys]; NSComparator sortBlock =^(id obj1,id obj2) { return [obj2 compare:obj1]; }; NSArray* sortArr =[array sortedArrayUsingComparator:sortBlock]; for(id obj in sortArr) { NSLog(@"%@:%@",obj,[dict objectForKey:obj]); } NSMutableArray* m =[[NSMutableArray alloc]init]; for(id ob in sortArr) { [m addObject:[dict objectForKey:ob]]; } NSLog(@"%@",m); NSString* join = [m componentsJoinedByString:@""]; NSLog(@"%@",join); } return 0; }