博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSMutable sort排序
阅读量:5215 次
发布时间:2019-06-14

本文共 2223 字,大约阅读时间需要 7 分钟。

Compare method

Either you implement a compare-method for your object:

- (NSComparisonResult)compare:(Person *)otherObject {    return [self.birthDate compare:otherObject.birthDate];}NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

NSSortDescriptor (better)

or usually even better:

NSSortDescriptor *sortDescriptor;sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"                                              ascending:YES] autorelease];NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];

You can easily sort by multiple keys by adding more than one to the array. Using custom comparator-methods is possible as well. Have a look at .

Blocks (shiny!)

There's also the possibility of sorting with a block since Mac OS X 10.6 and iOS 4:

NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {    NSDate *first = [(Person*)a birthDate];    NSDate *second = [(Person*)b birthDate];    return [first compare:second];}];
 

For this particular example I'm assuming that the objects in your array have a 'position' method, which returns an NSInteger.

NSArray *arrayToSort = where ever you get the array from... ;NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) {  if ([obj1 position] > [obj2 position]) {     return (NSComparisonResult)NSOrderedDescending;  }  if ([obj1 position] < [obj2 position]) {    return (NSComparisonResult)NSOrderedAscending;  }  return (NSComparisonResult)NSOrderedSame;};NSArray *sorted = [arrayToSort sortedArrayUsingComparator:sortBlock];

Note: the "sorted" array will be autoreleased.

If this 'position' is in NSDictory.

NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) {

            NSInteger p1= [((NSString*) [obj1 objectForKey:@"position"]) integerValue];

            NSInteger p2= [((NSString*) [obj2 objectForKey:@"position"]) integerValue];

            if (p1 > p2) {

                return (NSComparisonResult)NSOrderedDescending;

            }

            if (p1 < p2) {

                return (NSComparisonResult)NSOrderedAscending;

            }

            return (NSComparisonResult)NSOrderedSame;

        };

转载于:https://www.cnblogs.com/likwo/p/3983664.html

你可能感兴趣的文章
mysql二进制日志
查看>>
VLOOKUP 函数
查看>>
20165318 2017-2018-2 《Java程序设计》第一周学习总结
查看>>
mysql学习(2)-MySQL服务器优化
查看>>
H5版俄罗斯方块(2)---游戏的基本框架和实现
查看>>
【bzoj4300】绝世好题 dp
查看>>
【bzoj5180】[Baltic2016]Cities 斯坦纳树
查看>>
docker使用
查看>>
Javascript 获得元素位置&&获得元素样式值-网上找的,方便以后查阅
查看>>
【GUI】基于V7开发板的裸机和各种RTOS版本的emWin程序模板,支持硬件JPEG,已发布(2019-05-26)...
查看>>
物联网操作系统HelloX开发者入门指南
查看>>
Hello China操作系统的安装和使用
查看>>
一键生成http服务器
查看>>
[ActionScript 3.0] as3启动QQ
查看>>
对于 yii2 高级模板 生成文件入口
查看>>
C语言math.h库函数中atan与atan2的区别
查看>>
Bresenham算法
查看>>
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
查看>>
#pragma once
查看>>
MySQL 安装配置
查看>>