首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用我实现的比较器来获取这些数据

使用我实现的比较器来获取这些数据
EN

Stack Overflow用户
提问于 2016-10-17 13:35:36
回答 1查看 59关注 0票数 0

数据是.

活动名称:- HSS我们约会日期:- 6/10/2016 - 7/10/2016

事件名称:- CSS退出日期:- 6/10/2016 - 9/10/2016

活动名称:-数码公众会议日期:- 2016年10月6日至2016年10月7日

活动名称:- GEO会议日期:- 6/10/2016 - 7/10/2016

活动名称:- ISB会议日期:- 2016年10月6日至2016年10月7日.等

我已经使用时间戳对此进行了排序,但是现在由于我有相同的日期,我希望使用事件名称提升来排序这些数据,而不会影响我数组中的其他数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-17 14:50:57

假设你有这门课:

代码语言:javascript
复制
@interface EventInfo
//Note that calling a NSDate with timeStamp is weird, we may expect a NSTimerInverval
@property (nonatomic, strong) NSDate eventStartTimeStamp; 
@proprety (nonatomic, strong) NSDate eventName;
@end

您使用NSComparisonResult块。而不是简单地返回日期的比较,如果日期是相同的,则返回事件名称的比较(字母顺序)。

代码语言:javascript
复制
NSMutableArray *array = //Your array of EventInfo objects;
[array sortUsingComparator:^NSComparisonResult(EventInfo * _Nonnull event1, EventInfo * _Nonnull event2) {
    NSComparisonResult dateCompare = [event2.eventStartDateStamp compare:event1.eventStartDateStamp]; 
    /*Date are not the same => Date comparison is priority*/
    if (dateCompare != NSOrderedSame)
    {
       return dateCompare;
    }
    else/*Same date => Use Event name to sort*/
    {
        return [event2.eventName compare:event1.eventName];
        //or return [event1.eventName compare:event2.eventName]; depending on alphabetical or reversed 
    }
}];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40087877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档