【发布时间】:2016-02-19 15:48:16
【问题描述】:
我有一组自定义对象,其中包含 date 和 revenue。该对象名为Game。
我已经使用compare 对对象数组进行了排序:
- (NSArray*)sortByDate:(NSArray*)objects
{
NSArray *sorted = [objects sortedArrayUsingComparator:^(id firstObject, id secondObject) {
Game *firstGameObject = (Game*)firstObject;
Game *secondGameObject = (Game*)secondObject;
return [(NSDate*)firstGameObject.date compare:(NSDate*)secondGameObject.date];
}];
return sorted;
}
我想按日期对它们进行排序,然后将它们设置为按月和年(2013 年 4 月、2015 年 10 月等...)划分的 UITableView。
我尝试将日期排序为MMMM YYYY 格式,但它们在数组中添加的顺序错误。
- (NSArray*)getSectionsTitles:(NSArray*)objects
{
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale currentLocale];
dateFormatter.timeZone = calendar.timeZone;
[dateFormatter setDateFormat:@"MMMM YYYY"];
for (Game *game in objects)
{
NSString *sectionHeading = [dateFormatter stringFromDate:game.date];
[mutableSet addObject:sectionHeading];
}
return mutableSet.allObjects;
}
【问题讨论】:
-
您可以打印
mutableSet.allObjects以查看您收到的订单吗? -
因为 NSMutableSet 没有顺序。 link
标签: ios objective-c arrays sorting nsdate