`
alex09
  • 浏览: 968967 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

NSMutableArray 中排序

阅读更多
使用sortedArrayUsingComparator方法

NSArray *sorted = [_scores sortedArrayUsingComparator:^(id obj1, id obj2){
    if ([obj1 isKindOfClass:[Score class]] && [obj2 isKindOfClass:[Score class]]) {
        Score *s1 = (Score*)obj1;
        Score *s2 = (Score*)obj2;

        if (s1.points > s2.points) {
            return (NSComparisonResult)NSOrderedAscending;
        } else if (s1.points < s2.points) {
            return (NSComparisonResult)NSOrderedDescending;
        }
    }

    // TODO: default is the same?
    return (NSComparisonResult)NSOrderedSame;
}];


使用sortedArrayUsingSelector方法
- (NSComparisonResult)compare:(Person *)otherObject {
    return [self.birthDate compare:otherObject.birthDate];
}

NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];


使用sortedArrayUsingDescriptors:sortDescriptors
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
分享到:
评论

相关推荐

    iOS 对NSMutableArray进行排序和过滤的实例

    下面小编就为大家分享一篇iOS 对NSMutableArray进行排序和过滤的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    NSArray_NSMutableArray

    Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。

    NSMutableArray-SafeTransaction:NSMutableArray线程安全事务

    NSMutableArray-SafeTransaction NSMutableArray线程安全事务 NSMutableArray不是线程安全的,如果您向每个API添加锁定,则会被包围。 假设这样: 线程1获得可变数组的计数。 线程#2删除最后一个对象。 线程#...

    IOS 开发之 NSMutableArray与NSArray 的区别

    主要介绍了IOS 开发之 NSMutableArray与NSArray 的区别的相关资料,希望通过本文能掌握这部分内容,需要的朋友可以参考下

    NSMutableArray-Delegate

    NSMutableArray-Delegate 是为可变数组添加模式委托的类别。 支持的委托方法 - (void)mutableArray:(NSMutableArray *)array willAddObject:(id)object - (void)mutableArray:(NSMutableArray *)array didAddObject...

    Iphone数组一些基础操作 NSArray/NSMutableArray

    Iphone数组一些基础操作 NSArray/NSMutableArrayIphone

    NSMutableArray-Shuffle:NSMutableArray上的一个有用类别,提供了一种改组数组的方法

    用法在要添加随机播放功能的类中导入NSMutableArray + Shuffle.h文件。 # import " NSMutableArray+Shuffle.h " 创建NSMutableArray并在需要时在数组上调用shuffle方法。 NSMutableArray *shuffledArray = [[ ...

    长按拖动排序

    // stBtn.buttonArray = [[NSMutableArray alloc]init]; // for (NSInteger i = 0; i ; i++) { // UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i*375/4.0, 0, 375/4.0, 100)]; // [stBtn addSubview...

    iOS实现联系人按照首字母进行排序的实例

    联系人功能的需求一般都会有按照首字母排序,并且会要求同一个姓的就要连续起来中间不能穿插别的姓,百度了一下看到UILocalizedIndexedCollation给我们提供了很方便的排序方法,它不需要将中文转为拼音,但是有一个...

    safely安全使用NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSMutableString。-Swift开发

    功能安全访问NSArray安全访问NSDictionary安全访问并对NSMutableArray进行更改安全访问并对NSMutableDictionary进行更改安全访问并对NSMutableString进行更改要求iOS 8.0+ / macOS 10.13.6+ Xcode 10.1(10B61)+...

    Objective-C 经典字典数组排序 – 省市区

    3.在进行排序前,我们首先要先滤清思路:这答题的难题是在于如何将字典装入数组,再把数组装入字典,所以关键点就在于如何通过嵌套将每个字典与数组归到于它们相对应的数组与字典里 提供一个小思路:数组-&gt;字典-&gt;数组-&gt;...

    iOS 数据结构之数组的操作方法

    数组是线性结构是容器类型,是一块连续的内存空间, iOS 中用 NSArray 和 NSMutableArray 集合类型,用来存放对象类型,其中 NSArray是不可变类型, NSMutableArray 是可变类型,能够对数组中元素进行增删改查. ...

    ios自定义折线图(带动画效果)

    NSMutableArray *dataTitleArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; NSMutableArray *dataArray=[[NSMutableArray alloc] initWithObjects:@"86",@"2...

    OCGeneric:Objective-C中的基础通用支持

    NSMutableArray ( NSString ) *strAry = [[NSMutableArray( NSString ) alloc] init]; NSMutableArray ( NSNumber ) *numberAry = [[NSMutableArray( NSNumber ) alloc] init]; 类型识别 当您要将NSNumber对象...

    ios自定义柱形图(带动画效果)

    NSMutableArray *dataTitleArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; NSMutableArray *dataArray=[[NSMutableArray alloc] initWithObjects:@"86",@"2...

    ios-WebView实现网页图片获取放大.zip

    NSMutableArray *arrImgURL = [[NSMutableArray alloc] init]; int node = [self nodeCountOfTag:@"img"]; for (int i = 0; i ; i ) { NSString *jsString = [NSString stringWithFormat:@"document....

    ios-HcdGuideView引导页-通过简单的方法给app加入引导页面.zip

    NSMutableArray *images = [NSMutableArray new];[images addObject:[UIImage imageNamed:@"1"]];[images addObject:[UIImage imageNamed:@"2"]];[images addObject:[UIImage imageNamed:@"3"]];[...

    ios-城市选择器.zip

    cityListVC.arrayHotCity = [NSMutableArray arrayWithObjects:@"北京",@"上海",@"广州",@"厦门",@"深圳",@"天津",@"长沙",@"郑州", nil]; //历史选择城市列表 cityListVC.arrayHistoricalCity = ...

    自定义开关按钮(ios)

    NSMutableArray *unselectImages = [[NSMutableArray alloc]initWithObjects:normal_left, normal_right, nil]; [normal_left release]; [normal_right release]; UIImage *select_left = [[UIImage alloc] ...

    Foundation框架

    学习OC中的Foundation框架的总结,包括NSObject,NSString,NSMutableString,NSArray,NSMutableArray

Global site tag (gtag.js) - Google Analytics