我有这个返回NSArray日期的方法。问题是,当显示图表时,如果日期具有不同的年份,则日期的顺序不正确。
例如,如果有这些日期:12/25/13,01/18 / 14,13 / 28 / 13,02 / 22/14;该方法将返回如下:01/18 / 14,02 / 22 / 14,12 / 25 / 13,12 / 28/13。
我需要先按照年份订单,然后是月份,然后是一天订购日期。所以最终的数组应该是这样的:12/25/13,12/28/13,01/18/14,02/22/14。
所以任何人都可以指出代码有什么问题?
- (NSArray *)graphViewSortedMonths:(SimpleStockView *)graphView {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSArray *closingDates = [[DailyTradeInfoSource dailyTradeInfos] valueForKeyPath:@"tradingDate"];
//NSLog(@"%@", closingDates);
__weak NSCountedSet *months = [NSCountedSet set];
[closingDates enumerateObjectsUsingBlock:^(id closingDate, NSUInteger index, BOOL *stop) {
[months addObject:[calendar components:NSMonthCalendarUnit fromDate:closingDate]];
}];
//NSLog(@"%@", months);
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"month" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSLog(@"Months %@", [months sortedArrayUsingDescriptors:descriptors]);
return [months sortedArrayUsingDescriptors:descriptors];
}
这是我在NSLog时得到的:
014-03-24 04:10:48.697 SimpleStocks[6343:60b] Months (
"<NSDateComponents: 0x8c34af0>\n Month: 1\n Leap month: no",
"<NSDateComponents: 0x8c34c50>\n Month: 2\n Leap month: no",
"<NSDateComponents: 0x8c34cb0>\n Month: 3\n Leap month: no",
"<NSDateComponents: 0x8c349d0>\n Month: 12\n Leap month: no"
)
它应该是:
014-03-24 04:10:48.697 SimpleStocks[6343:60b] Months (
"<NSDateComponents: 0x8c349d0>\n Month: 12\n Leap month: no"
"<NSDateComponents: 0x8c34af0>\n Month: 1\n Leap month: no",
"<NSDateComponents: 0x8c34c50>\n Month: 2\n Leap month: no",
"<NSDateComponents: 0x8c34cb0>\n Month: 3\n Leap month: no",
)
这就是我得到的:
这是更正后的代码:
答案 0 :(得分:3)
我认为问题在于您只从日期数组中提取月份DateComponents。如果您只按月份排序,它们将按您所看到的顺序排列:1,2,3,12。
我认为,您想要提取月份和年份,然后按年份排序,然后按月份排序:
- (NSArray *)graphViewSortedMonths:(SimpleStockView *)graphView
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSArray *closingDates = [[DailyTradeInfoSource dailyTradeInfos] valueForKeyPath:@"tradingDate"];
NSCountedSet *monthsAndYears = [NSCountedSet set];
[closingDates enumerateObjectsUsingBlock:^(id closingDate, NSUInteger index, BOOL *stop) {
[monthsAndYears addObject:[calendar components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:closingDate]];
}];
NSSortDescriptor *monthDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"month" ascending:YES];
NSSortDescriptor *yearDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:YES];
NSArray *descriptors = @[ yearDescriptor, monthDescriptor ];
NSArray *result = [monthsAndYears sortedArrayUsingDescriptors:descriptors];
NSLog(@"Months %@", [result valueForKeyPath:@"month"]);
return result;
}
答案 1 :(得分:-1)
如果您有日期对象数组,那么您可以使用如下所示。
- (NSMutableArray *)graphViewSortedMonths:(SimpleStockView *)graphView {
NSMutableArray *dateObjects = [NSMutableArray alloc] init];
NSArray *closingDates = [[DailyTradeInfoSource dailyTradeInfos] valueForKeyPath:@"tradingDate"];
[closingDates enumerateObjectsUsingBlock:^(id closingDate, NSUInteger index, BOOL *stop) {
[dateObjects addObject:closingDate];
}];
NSLog(@"LOG:%@", dateObjects);
[dateObjects sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
return ([obj1 compare:obj2] == NSOrderedAscending);
}];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSMutableArray *months = [NSMutableArray alloc] init];
for(NSDate *obj in dateObjects){
[months addObject:[calendar components:NSMonthCalendarUnit fromDate:obj]];
}
return months;
}