這幾天要做一個物件的排序,結果發現 OC 的排序只能針對基本型別做排序,
如果我的 array 是放 customer object 就必須要另外實作。
這實在讓我太苦惱了啊~~~
NSArray *arrRestaurantQueue = [[dicResult objectForKey:@"RestaurantRsv"] sortedArrayUsingComparator:^NSComparisonResult(QDReservation *obj1, QDReservation *obj2) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date1 = [dateFormatter dateFromString:obj1.rsvTime];
NSDate *date2 = [dateFormatter dateFromString:obj2.rsvTime];
if ([date1 compare:date2] == NSOrderedDescending) {
return (NSComparisonResult)NSOrderedDescending;
} else if ([date1 compare:date2] == NSOrderedAscending) {
return (NSComparisonResult)NSOrderedAscending;
} else {
return (NSComparisonResult)NSOrderedSame;
}
}];