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 ...
随机推荐
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 【论文笔记】Social Role-Aware Emotion Contagion in Image Social Networks
Social Role-Aware Emotion Contagion in Image Social Networks 社会角色意识情绪在形象社交网络中的传染 1.摘要: 心理学理论认为,情绪代表了 ...
- JAVA-JSP内置对象之session对象
相关资料:<21天学通Java Web开发> session对象 1.session对象用来表示用户的会话状况,一般用于保存用户的各种信息.2.直到生命周期超时或者被认为释放掉为止. 方法 ...
- phalcon无限重定向
问题 换了个新电脑,后来重新配置phalcon环境.由于用得是windows,而且还用得是2.0.5得版本,官网已经没提供这个版本下载了.而旧电脑已经被格式化了,
- [转]Oracle的update语句优化研究
原文地址:http://blog.csdn.net/u011721927/article/details/39228001 一. update语句的语法与原理 1. 语法 单表 ...
- oracle本月、上月、去年
select trunc(sysdate, 'month') 本月第一天, trunc(last_day(sysdate)) 本月最后一天, trunc(add_month ...
- centos 6.5 升级php到5.6.17版本
1. 下载php5.6.17版本:编译安装, ./configure --prefix=/usr/local/php5. --with-config-/etc --with-mysql=/usr/lo ...
- C#使用线程池创建线程
using System; using System.Threading; public class Example { public static void Main() { // Queue th ...
- python入门-分类和回归各种初级算法
引自:http://www.cnblogs.com/taichu/p/5251332.html ########################### #说明: # 撰写本文的原因是,笔者在研究博文“ ...
- 01,WP8开发基础
1,wp应用程序的生命周期 编写wp程序,首先必须要搞懂程序的生命周期,因为我们需要在这些不同的时间点做一些必要的事情,比如保存和恢复一些数据.下面这几个事件是我们必须关注的. Applicatoin ...