NSArray *numberArray = @[
@"
1
"
,
@"
5
"
,
@"
45
"
,
@"
17
"
,
@"
21
"
];
//
block比较方法,数组中可以是NSInteger,NSString(需要转换)
NSComparator finderSort = ^(
id
string1,
id
string2){
if
([string1 integerValue] >
[string2 integerValue]) {
return
(NSComparisonResult)NSOrderedDescending;
}
else
if
([string1 integerValue] <
[string2 integerValue]){
return
(NSComparisonResult)NSOrderedAscending;
}
else
return
(NSComparisonResult)NSOrderedSame;
};
//
数组排序:
NSArray *resultArray =
[numberArray sortedArrayUsingComparator:finderSort];
NSLog(
@"
第一种排序结果:%@
"
,resultArray);