Reputation: 137
Data are ... ... ...
Event name :- HSS WE Date :- 6/10/2016 - 7/10/2016
Event name :- CSS Retreat Date :- 6/10/2016 - 9/10/2016
Event name :- Digital Public Conference Date :- 6 October 2016 - 7 October 2016
Event name :- GEO Conference Date :- 6/10/2016 - 7/10/2016
Event name :- ISB Conference Date :- 6 October 2016 - 7 October 2016 ... ... ... etc
I have sorted this using time Stamp, But now as I have same date, I want to sort this data with event name Ascending without affecting other datas in my array.
Upvotes: 0
Views: 66
Reputation: 26026
Let's say that you have this class:
@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
You use a NSComparisonResult
block.
Instead of simply return the comparison of the dates, if is the date is the same, return then the comparison (alphabetical) with the event name.
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
}
}];
Upvotes: 1