先讲解一点小知识:适用于数字,字母排序
NSOrderedAscending的意思是:左边的操作对象小于右边的对象。
NSOrderedDescending的意思是:左边的操作对象大于右边的对象。
NSArray *array = [NSArray arrayWithObjects:@"password",@"username",@"token",@"num", nil];
//普通排序系统自带的升序
NSArray *sortedArray1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后1:%@",sortedArray1);
NSArray *sortedArray2 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
//逆转
return NSOrderedDescending;
}];
NSLog(@"排序后2:%@",sortedArray2);
NSArray *sortedArray3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
//不逆转
return NSOrderedAscending;
}];
NSLog(@"排序后3:%@",sortedArray3);
NSArray *sortedArray4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//升序
NSComparisonResult result = [obj1 compare:obj2];
return result;
}];
NSLog(@"排序后4:%@",sortedArray4);
NSArray *sortedArray5 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//降序
NSComparisonResult result = [obj1 compare:obj2];
return -result;
}];
NSLog(@"排序后5:%@",sortedArray5);
运行结果:
大家根据自身的情况选择一种合适的方法!!!!!