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 ...
随机推荐
- 【密码学】RSA公钥密码体制
RSA公钥密码体制是美国麻省理工学院(MIT)的三位科学家Rivest.Shamir.Adleman于1978年提出的,简称RSA公钥秘密系统.实际上,RSA稍后于MH背包公钥密码实用系统,但它的影响 ...
- WPF Image控件 Source: Byte[] ,BitmapImage 相互转换
文件转为byte[] FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); byte[] desBytes ...
- JAVA-JSP内置对象之out对象进行页面输出
相关资料:<21天学通Java Web开发> out对象 out对象进行页面输出1.通过out对象的print()方法和println()方法进行页而输出.2.不同的println()方法 ...
- C#使用双缓存减少界面闪烁
场景:一个panel中动态加载多个自定义控件item,类似QQ聊天窗口 问题:加载panel时界面会卡顿,先显示阴影再显示界面:移动滚动条时item会闪烁 解决方法: panel 添加方法,减少界面闪 ...
- JavaScript 中 类型转换
转自 https://www.cnblogs.com/wuxiaoshang/p/5835627.html // 转换成字符型 var married = false; alert(married.t ...
- PAGED_CODE()
#if DBG #define PAGED_CODE() \ /*APC_LEVEL*/) { \ VideoPortDebugPrint(, "Video: Pageable code c ...
- hbase源码系列(十一)Put、Delete在服务端是如何处理?
在讲完之后HFile和HLog之后,今天我想分享是Put在Region Server经历些了什么?相信前面看了<HTable探秘>的朋友都会有印象,没看过的建议回去先看看,Put是通过Mu ...
- 阿里云免费SSL证书绑定+sever2012 IIS配置
1.阿里云域名 2.点击证书 3.免费证书 4.下载证书 5.服务器-运行-mmc 进入制台程序 6.制台程序,选择菜单“文件"中的"添加/删除管理单元”-> “添加”,从“ ...
- VB6学习笔记
1.数据库读取 [工程]菜单的[引用]菜单项,打开引用对话框,选中[Microsoft ActiveX Data Objects 6.1 Library] [工程]菜单的[引用]菜单项,打开引用对话框 ...
- 百度地图Api进阶教程-默认控件和自定义控件2.html
<!DOCTYPE html> <html> <head> <meta name="viewport" content="ini ...