2016-10-17 28 views
0

的數據是 ... ... ...使用比較我實現得到這個數據

事件名稱: - HSS WE 日期: - 2016年6月10日 - 2016年7月10日

事件名稱: - CSS撤退 日期: - 2016年6月10日 - 2016年9月10日

事件名稱: - 數字公共會議 日期: - 2016年10月6日 - 2016年10月7日

事件名稱: - GEO會議 日期: - 2016年6月10日 - 2016年7月10日

事件名稱: - ISB會議 日期: - 2016年10月6日 - 2016年10月7日 ... .. ...等

我已經使用時間戳排序,但現在,因爲我有相同的日期,我想排序這個數據與事件名稱升序而不影響其他數據在我的數組中。

+0

顯示你的代碼,在這裏顯示事件包含數組。 – vaibhav

+0

你是怎麼分類的?您可以指定要排序的各種關鍵字,或者如果您使用了比較器塊,如果日期相同,則可以按名稱排序。 – Larme

+0

NSComparisonResult dateEvent1SortDescending(EventInfo * event1,EventInfo * event2,void * context){ return [event2.eventStartDateStamp compare:event1.eventStartDateStamp]; } – Roshanboy4u

回答

0

比方說,你有這樣的類:

@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 
    } 
}]; 
+0

事件名稱: - HSS WE日期: - 2016年6月10日 - 2016年7月7日 這仍然是在同一個地方.. 它應該在 事件名稱: - GEO會議日期: - 6/10/2016 - 2016年7月7日 – Roshanboy4u

+0

'return [event1.eventName compare:event2.eventName]'可能會更好?和我不匹配的方法('sortedArray ...'和'sortUsing ...',一個應用在同一個可變數組上,另一個返回排序後的數組。 – Larme

+0

沒有工作。:( – Roshanboy4u