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 NSMutableArrayby 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 ...
随机推荐
- js实现复制功能,将需要复制的内容放入剪切板上
方法一:使用ZeroClipboard.js插件 <html> <head> <meta charset="UTF-8"> </head& ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- C#学习笔记(29)——Linq的实现,Lambda求偶数和水仙花数
说明(2017-11-22 18:15:48): 1. Lambda表达式里面用了匿名委托,感觉理解起来还是挺难的.求偶数的例子模拟了Linq查询里的一个where方法. 2. 蒋坤说求水仙花数那个例 ...
- 2. 知识图谱-命名实体识别(NER)详解
1. 通俗易懂解释知识图谱(Knowledge Graph) 2. 知识图谱-命名实体识别(NER)详解 3. 哈工大LTP解析 1. 前言 在解了知识图谱的全貌之后,我们现在慢慢的开始深入的学习知识 ...
- CAD技巧之001——如何将单个字合并起来
CAD技巧之001——如何将单个字合并起来 很多同志如果遇到奇葩的单个文字合并,怎么办,重新书写?复制粘贴?如果工作量很大,怎么办呢. 今天九天就教您一个好方法! 废话不多说,上动画片
- 【Unity Shader】一、顶点函数(vertex)和片元函数(fragment)
学习资料:http://www.sikiedu.com/course/37/task/430/show 学习Shader中顶点函数(vertex)和片元函数(fragment)的基本用法. Shade ...
- C语言 · 学做菜
算法训练 学做菜 时间限制:1.0s 内存限制:256.0MB 问题描述 涛涛立志要做新好青年,他最近在学做菜.由于技术还很生疏,他只会用鸡蛋,西红柿,鸡丁,辣酱这四种原料来做菜,我 ...
- Scrum卡片层次图
对照国内的项目管理软件禅道,可以好好感受一下,何为Scrum. 看板则一定要是实物,才有感觉.
- tomcat出现的PermGen Space问题<转>
最近做项目碰到了让我纠结的问题,tomcat服务器运行一段时间,总是会自动报异常:java.lang.OutOfmemoryError: PermGen Space 的错误,导致项目无法正常运行. 出 ...
- 【oneday_onepage】——How to stretch the life of your SSD storage
How to stretch the life of your SSD storage July 18, 2013, 9:06 PM — Once a PC enthusiast's dream st ...