iOS: Sorted Array with Compare
Question(提问):
What I want to do seems pretty simple, but I can't find any answers on the web. I have an NSMutableArray
of objects, let's say they are 'Person' objects. I want to sort the NSMutableArray
by Person.birthDate which is an NSDate
.
I think it has something to do with this method:
NSArray*sortedArray =[drinkDetails sortedArrayUsingSelector:@selector(???)];
In Java I would make my object implement Comparable, or use Collections.sort with an inline custom comparator...how on earth do you do this in Objective-C?
Anwser(回答):
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];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 the documentation.
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];
}];
转自:http://stackoverflow.com/a/805589(Stack Overflow)
iOS: Sorted Array with Compare的更多相关文章
- 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode#154]Find Minimum in Rotated Sorted Array II
The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- [Swift]LeetCode80. 删除排序数组中的重复项 II | Remove Duplicates from Sorted Array II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- FB面经Prepare: Merge K sorted Array
Merge K sorted Array 跟Merge K sorted lists不同在于,从PQ里poll出来以后不知道下一个需要被加入PQ的是哪一个 所以需要写一个wrapper class p ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- Find Minimum in Rotated Sorted Array I & II
Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
随机推荐
- Oracle 大小写转换函数
Oracle 大小写转换函数 转大写UPPER 转小写LOWER 测试: select UPPER('Test') as u from dual; select LOWER('Test') as l ...
- maven invalid loc header
项目部署之后出现如题错误 清除maven资源库原有的jar,重新下载即可解决
- Oracle报错:ORA-06508: PL/SQL: 无法找到正在调用的程序单元
原因:调用的存储过程已经被修改过. 解决方案:重新编译存储过程.
- Spring security 用户,角色,权限,资源
转自:http://blog.csdn.net/wybqq/article/details/52940194 关于Spring security对用户请求的处理过程 体现在这两个过程的体现. 关于用户 ...
- A. Counterexample (Codeforces Round #275(div2)
A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- InstallShield安装jdk并设置环境变量
1. 检查是否安装jdk function OnBegin() begin Disable (BACKBUTTON); if(!MAINTENANCE)then SdLicense2 ("L ...
- LINUX任务(jobs)详解
LINUX任务(jobs)详解 在用管理员执行一个命令后,用Ctrl+Z把命令转移到了后台.导致无法退出root的. 输入命令:exit终端显示:There are stopped jobs. 解决方 ...
- JS学习 - offset家族(一)
JS学习 - offset家族(一) 先来张图开开胃 offsetWidth offetHeight 得到对象的宽度和高度(自己的,与他人无关) offsetWidth = width + borde ...
- PCL行人检测
首先我们知道Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功,HOG+SVM进行行人检测的方法是法国研究人员Dalal在2005的CVPR上提出的,而如今虽然有很 ...
- MFC函数——CWnd::OnEraseBkgnd
CWnd::OnEraseBkgnd afx_msg BOOL OnEraseBkgnd( CDC* pDC ); 返回值: 如果它擦除了背景,则返回非零值:否则返回0. 参数: pDC 指定了设备环 ...