第一种:利用数组的sortedArrayUsingComparator调用 NSComparator 

示例:

obj1obj2指的是数组中的对象

//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。

这个方法中的obj1obj2分别是指数组中的对象。

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];

//

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

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 数组排序的更多相关文章

  1. iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等

    [ZOYSessionManager dataWithUrlString:GetVideoDataComment andParameter:@{@"id":userID,@&quo ...

  2. iOS数组排序

    [_fields sortUsingComparator:^NSComparisonResult(UITextField *obj1, UITextField *obj2) { /* NSOrdere ...

  3. IOS数组排序等

    一.UITextField的代理方法 #pragma mark 当文本框开始编辑的时候调用---开始聚焦 - (void)textFieldDidBeginEditing:(UITextField * ...

  4. iOS数组使用

    相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...

  5. IOS对存放对象的数组排序

    我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天程序则又一个 Friend类,点菜程序会有一个Recipe类等.有时候我们希望 ...

  6. iOS学习16之OC集合遍历和数组排序

    1.集合遍历 1> 遍历 集合(Collection):OC中提供的容器类:数组,字典,集合. 遍历:对集合中元素依次取出的过称叫做遍历. 三种方式:① for循环遍历: ② NSEnumera ...

  7. iOS学习之Object-C语言集合遍历和数组排序

    一.集合遍历      1.集合:OC中提供的容器类,数组,字典,集合.      2.遍历:对集合中元素依次取出的过程叫做遍历. 二.for循环遍历      1.通过for循环的循环变量用作数组元 ...

  8. iOS之NSArray数组排序

    一.数组遍历 除了常用的for和for-in遍历外,系统还提供了三种枚举遍历,对于大量的数据遍历可以使用下列三个方法. - (void)enumerateObjectsUsingBlock:(void ...

  9. iOS 二维数组排序小算法

    NSArray *tmp = @[@[@(1), @(2), @(3), @(4), @(5)],                     @[@(6), @(7), @(8), @(9), @(10 ...

随机推荐

  1. [stm32] 利用uc-gui封装画图和画线函数移植51上的模拟动画

    >_<:这里的动画是黄色矩形区域中一个模仿俯视图的起重机运作动画,一个是模仿主视图的吊钩的运动.通过改变初始Init函数中的数据b_x,b_y实现矩形区域的移动.当实时采集时要首先根据起重 ...

  2. 说不尽的MVVM(2) – MVVM初体验

    知识预备 阅读本文,我假定你已经具备以下知识: C#.WPF基础知识 了解Lambda表达式和TPL 对事件驱动模型的了解 知道ICommand接口 发生了什么 某程序员接到一个需求,编写一个媒体渲染 ...

  3. 如何成为Python高手(转载)

    本文是从 How to become a proficient Python programmer 这篇文章翻译而来. 这篇文章主要是对我收集的一些文章的摘要.因为已经有很多比我有才华的人写出了大量关 ...

  4. 浅谈压缩感知(二十六):压缩感知重构算法之分段弱正交匹配追踪(SWOMP)

    主要内容: SWOMP的算法流程 SWOMP的MATLAB实现 一维信号的实验与结果 门限参数a.测量数M与重构成功概率关系的实验与结果 SWOMP与StOMP性能比较 一.SWOMP的算法流程 分段 ...

  5. 记一次https访问握手失败(handshake failure)

    文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6239518.html  转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...

  6. paip.日期时间操作以及时间戳uapi php java python 总结

    paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...

  7. mysql优化之表建设

    就拿常见的用户表.文章类的表.日志表来分析如下 CREATE TABLE `user` (   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMEN ...

  8. lamper技能树

  9. [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler

    [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler 1 官方网站:http://memprofiler.com/2 下载地址:http://memprofiler. ...

  10. ArrayList/Vector的原理、线程安全和迭代Fail-Fast

    疑问 * ArrayList是非线程非安全的,具体是指什么?具体会产生什么问题?* ArrayList的内部原理是什么?为什么可以动态扩容?* Vector是线程安全的,具体是如何实现的?为什么不再推 ...