ios 数组排序
第一种:利用数组的sortedArrayUsingComparator调用 NSComparator
示例:
obj1和obj2指的是数组中的对象
//1.数组中存放的是字符
NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
NSLog(@"排序前:%@",sortArray);
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSLog(@"排序后:%@",array);
//2.数组中存放的是字典对象
NSComparator cmptr = ^(id obj1,id obj2) {
if ([[obj1 objectForKey:@"date_update"] intValue] > [[obj2 objectForKey:@"date_update"] intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[obj1 objectForKey:@"date_update"] intValue] < [[obj2 objectForKey:@"date_update"] intValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};
favoritesArray为NSArray数组,内容为:(
{
"date_update" = 1374823501;
id = 2;
videolink = "http://v.youku.com/player/getRealM3U8/vid/XNTY5NzcxNTAw/type//video.m3u8";
},
{
"date_update" = 1375177741;
id = 1;
videolink = "http://v.youku.com/player/getRealM3U8/vid/XNTY5MjcwNDcy/type//video.m3u8";
},
{
"date_update" = 1374824546;
id = 3;
videolink = "http://v.youku.com/player/getRealM3U8/vid/XNTY5Njk0Mjg0/type//video.m3u8";
}
)
favoritesArray = [favoritesArray sortedArrayUsingComparator:cmptr];
NSLog(@"排序后 array ===%@",favoritesArray);
第二种排序方法:利用sortedArrayUsingFunction 调用对应方法customSort。
这个方法中的obj1和obj2分别是指数组中的对象。
NSInteger customSort(id obj1, id obj2, void *context)
{
if ([[obj1 objectForKey:@"date_update"] integerValue] > [[obj2 objectForKey:@"date_update"] integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[obj1 objectForKey:@"date_update"] integerValue] < [[obj2 objectForKey:@"date_update"] integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}
favoritesArray = [favoritesArray sortedArrayUsingFunction:customSort context:nil];
//
NSInteger intSort(id num1, id num2,void*context){
int v1 =[num1 intValue];int v2 =[num2 intValue];
if(v1 < v2)returnNSOrderedAscending;
elseif(v1 > v2)returnNSOrderedDescending;
elsereturnNSOrderedSame;
}
NSArray*sorted_bookings =[myUnsortedArray sortedArrayUsingFunction:Sort_Bookingdate_Comparer context:self]; NSIntegerSort_Bookingdate_Comparer(id id1, id id2,void*context){// Sort
FunctionBooking* booking1 =(Booking*)id1;
Booking* booking2 =(Booking*)id2;
return([booking1.BOOKING_DATE compare:booking2.BOOKING_DATE]);
}
This I used to sort bookings by bookingdate. Booking is a class with a synthesized instance variable called BOOKING_DATE.
[opponentMatchDicts sortUsingFunction:compareMatchByDate context:nil];
static int compareMatchByDate( id m1, id m2,void*context){
NSDictionary*mDict1 =(NSDictionary*) m1;
NSDictionary*mDict2 =(NSDictionary*) m2;
NSDate*date1 =[mDict1 objectForKey:kMatchNSDate];
NSDate*date2 =[mDict2 objectForKey:kMatchNSDate];
int rv =[date1 compare:date2];
return rv;
}
//
第三种利用sortUsingDescriptors调用NSSortDescriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date_update" ascending:NO];
//其中,date_update为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便.
//1.对数组中的字典对象排序
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date_update" ascending:YES];
[favoritesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSLog(@"排序后 favoritesArray===%@",favoritesArray);
//2.可变数组
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for (int i=0; i<10; i++) {
NSString *str = [NSString stringWithFormat:@"%d",i];
NSMutableDictionary *httpHeaderDic = [[NSMutableDictionary alloc]initWithCapacity:2];
[httpHeaderDic setValue:str forKey:@"id"];
[tempArray addObject:httpHeaderDic];
}
NSLog(@"排序前 Array ==%@",tempArray);
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:NO];
[tempArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor2]];
NSLog(@"排序后 Array ==%@",tempArray);
ios 数组排序的更多相关文章
- iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等
[ZOYSessionManager dataWithUrlString:GetVideoDataComment andParameter:@{@"id":userID,@&quo ...
- iOS数组排序
[_fields sortUsingComparator:^NSComparisonResult(UITextField *obj1, UITextField *obj2) { /* NSOrdere ...
- IOS数组排序等
一.UITextField的代理方法 #pragma mark 当文本框开始编辑的时候调用---开始聚焦 - (void)textFieldDidBeginEditing:(UITextField * ...
- iOS数组使用
相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...
- IOS对存放对象的数组排序
我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天程序则又一个 Friend类,点菜程序会有一个Recipe类等.有时候我们希望 ...
- iOS学习16之OC集合遍历和数组排序
1.集合遍历 1> 遍历 集合(Collection):OC中提供的容器类:数组,字典,集合. 遍历:对集合中元素依次取出的过称叫做遍历. 三种方式:① for循环遍历: ② NSEnumera ...
- iOS学习之Object-C语言集合遍历和数组排序
一.集合遍历 1.集合:OC中提供的容器类,数组,字典,集合. 2.遍历:对集合中元素依次取出的过程叫做遍历. 二.for循环遍历 1.通过for循环的循环变量用作数组元 ...
- iOS之NSArray数组排序
一.数组遍历 除了常用的for和for-in遍历外,系统还提供了三种枚举遍历,对于大量的数据遍历可以使用下列三个方法. - (void)enumerateObjectsUsingBlock:(void ...
- iOS 二维数组排序小算法
NSArray *tmp = @[@[@(1), @(2), @(3), @(4), @(5)], @[@(6), @(7), @(8), @(9), @(10 ...
随机推荐
- java集合——题4,6
4.(List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列. 例如: List list = new ArrayList(); list.add(“Hel ...
- hdu 1864 01背包 最大报销额
http://acm.hdu.edu.cn/showproblem.php?pid=1864 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- 聊聊 App Store 的产品和推广运营攻略
在这个工匠的时代,越来越多开发者投入了手机应用的开发圈子.如何才能在激烈的竞争中脱颖而出,少走弯路呢?我们跟空中网负责iPhone游戏的运营和推广的洪亮进行了一次交流,得到了不少经验和诀窍,值得分享给 ...
- javascript设计模式与开发实践阅读笔记(9)——命令模式
命令模式:有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么,此时希望用一种松耦合的方式来设计软件,使得请求发送者和请求接收者能够消除彼此之间的耦合关系. 说法很复 ...
- Windows Server 安装 BitLocker
打开PowerShell(管理员): C:\> Install-WindowsFeature BitLocker -Restart 安装好后,系统会自动重新启动. Windows Server ...
- 关于linux curl 地址参数的问题
例如 url 为 http://mywebsite.com/index.PHP?a=1&b=2&c=3web形式下访问url地址,使用$_GET是可以获取到所有的参数然而在Linux下 ...
- JQ中 trigger()和triggerHandler()区别
既然使用了trigger和triggerHandler,那么你应该了解了他们的差别了. trigger():在每一个匹配的元素上触发某类事件. triggerHandler():这个特别的方法将会触发 ...
- jQuery单选框radio绑定click事件
<div class="con_head"> <label><input type="radio" name="ask& ...
- 關於imagick不得不說的一些事
PHP建圖通常都用GD庫,因為是內置的不需要在服務器上額外安裝插件,所以用起來比較省心,但是如果你的程序主要的功能就是處理圖像,那麼就不建議用GD了,因為GD不但低效能而且能力也比較弱,佔用的系統資源 ...
- [salesforce] standard button
Use Case In Salesforce, when you click on the standard ‘New’ button on a Related List to create a ne ...