int main(int argc, const char * argv[]) { @autoreleasepool { //字符串进行排序 NSArray *[email protected][@"b",@"a",@"c"]; NSSortDescriptor *ns=[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]; NSArray *B=[arr sortedArrayUsingDescriptors:@[ns]]; NSLog(@"%@",B); //代码块排序 NSArray *a= [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }]; NSLog(@"%@",a); //选择器排序 NSArray *C=[arr sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",C); } return 0; } //可变集合的排序 NSMutableArray *arr=[NSMutableArray array]; [arr addObject:@"v"]; [arr addObject:@"b"]; [arr addObject:@"A"]; //选择器排序 [arr sortUsingSelector:@selector(compare:)]; NSLog(@"%@",arr); //代码块排序 [arr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }]; NSLog(@"%@",arr); //描述信息排序 NSSortDescriptor *nsSort=[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]; [arr sortUsingDescriptors:@[nsSort]]; NSLog(@"%@",arr);
时间: 2024-10-31 16:19:53