数组去重以及快速排序

去掉数组中的重复元素:

NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];

NSSet *set = [NSSet setWithArray:arr];

NSLog(@"set%@\n", set);

打印出来的结果:

{(

"12-11",

"12-14",

"12-13",

"12-12"

)}

对结果进行有序排列:

NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];

NSArray *sortSetArray = [set sortedArrayUsingDescriptors:sortDesc];

NSLog(@"set%@\nsort set%@", set, sortSetArray);

打印出来结果:

sort set(

"12-11",

"12-12",

"12-13",

"12-14"

)






想起来一直用NSArray的排序代码

NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];

NSArray *sort2Array = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

return [obj1 localizedCompare:obj2];

}];

NSLog(@"sort2 array %@", sort2Array);

//本地化比较

- (NSComparisonResult)localizedCompare:(NSString *)string;

//本地化比较(不区分大小写)

- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;

你可能感兴趣的:(数组去重以及快速排序)