数据是.
活动名称:- 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日.等
我已经使用时间戳对此进行了排序,但是现在由于我有相同的日期,我希望使用事件名称提升来排序这些数据,而不会影响我数组中的其他数据。
发布于 2016-10-17 14:50:57
假设你有这门课:
@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块。而不是简单地返回日期的比较,如果日期是相同的,则返回事件名称的比较(字母顺序)。
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
}
}];https://stackoverflow.com/questions/40087877
复制相似问题