//
// main.m
// 降序排序.m
//
// Created by FuHeXiang on 16/1/10.
// Copyright (c) 2016年 FuHeXiang. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 按照中文排序
//降序
NSLog(@"降序————————————————————————————————————————————————");
NSArray *arr=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"];
NSArray*arr2=[arr sortedArrayUsingSelector:@selector(localizedCompare:)];
for (id NUM in arr2) {
NSLog(@"%@",NUM);
}
//升序
NSLog(@"升序————————————————————————————————————————————————");
NSArray *arr3=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"];
NSArray*arr4=[arr3 sortedArrayUsingSelector:@selector(compare:)];
for (id num in arr4) {
NSLog(@"%@",num);
NSLog(@"根据自己的方法进行升序————————————————————————————————————————————————");
//根据自己的选择进行升序或者降序
NSComparator sort=^(id obj1 ,id obj2)
{
NSComparisonResult result =[obj1 compare:obj2];
return result ;
};
NSArray*sort2=[arr sortedArrayUsingComparator:sort ];
for (id STR in arr)
{
NSLog(@"%@",STR);
}
NSLog(@"根据自己的方法进行降序————————————————————————————————————————————————");
NSComparator sort22=^(id obj1 ,id obj2)
{
NSComparisonResult result1 =[obj2 compare:obj1];
return result1 ;
};
NSArray*sort23=[arr sortedArrayUsingComparator:sort ];
for (id STR in arr)
{
NSLog(@"%@",STR);
}
}
}
return 0;
}
原文:http://www.cnblogs.com/xzz5211314/p/5118499.html