Ios 如何在目标c中使用NSSortDescriptor按升序或降序对月份名称进行排序

Ios 如何在目标c中使用NSSortDescriptor按升序或降序对月份名称进行排序,ios,objective-c,xcode,nsmutablearray,nssortdescriptor,Ios,Objective C,Xcode,Nsmutablearray,Nssortdescriptor,我需要使用NSSortDescriptor按升序或降序对月份和年份进行排序我有一个数组 { month = Dec; year = 2017; }, { month = Oct; year = 2017; }, { month = Jan; year = 2018; } 我曾经做过这个代码 NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"year" ascending:YES]; N

我需要使用NSSortDescriptor按升序或降序对月份和年份进行排序我有一个数组

{
month = Dec;
year = 2017;
},
{
month = Oct;
year = 2017;
},
{
month = Jan;
year = 2018;
}
我曾经做过这个代码

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"year" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    yeardata = [NSMutableArray arrayWithArray:[yeardata sortedArrayUsingDescriptors:sortDescriptors]];

    for (NSDictionary * dic in yeardata)
    {
        NSString * stryear = [dic objectForKey:@"year"];
        NSString * strmonth = [dic objectForKey:@"month"];
        [year addObject:[NSString stringWithFormat:@"%@ , %@",strmonth,stryear]];
    }
我需要对数据进行排序 2017年2月 2017年3月 2017年6月 2017年8月 2018年1月

NSArray *array = @[@{
                       @"month":@"Dec",
                       @"year": @"2017",
                       },
                   @{
                       @"month":@"Oct",
                       @"year": @"2017",
                       },
                   @{
                       @"month":@"Jan",
                       @"year": @"2018",
                   }];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM"];

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary * _Nonnull dict1, NSDictionary * _Nonnull dict2) {

    NSDate *date1 = [dateFormatter dateFromString:dict1[@"month"]];
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:date1];
    [components1 setYear:[dict1[@"year"] integerValue]];
    NSDate *date2 = [dateFormatter dateFromString:dict2[@"month"]];
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:date2];
    [components2 setYear:[dict2[@"year"] integerValue]];
    NSDate *finalDate1 = [[NSCalendar currentCalendar] dateFromComponents:components1];
    NSDate *finalDate2 = [[NSCalendar currentCalendar] dateFromComponents:components2];
    return [finalDate1 compare:finalDate2];
}];

NSLog(@"Sorted: %@", sorted);
输出:

$>Sorted: (
        {
        month = Oct;
        year = 2017;
    },
        {
        month = Dec;
        year = 2017;
    },
        {
        month = Jan;
        year = 2018;
    }
)
那么逻辑是什么呢?
你不能用一个简单的
nssortddescriptor
,因为你的月份是字母,按字母顺序,“Dec”在“Oct”之前,而不是“time”的顺序。 因此,您需要创建一个可以简单比较的
NSDate
。 如果需要,您可以将该日期保存在数组的字典中,或者使用带有块的
sortedarray usingcomparator:

要颠倒顺序,块中最简单的方法是:

return [finalDate1 compare:finalDate2];


注意:构造
finalDate1
/
finalDate2
的方法可能不是最佳方法。

最好将月份存储为整数,然后只显示名称(短或长)当显示给用户时。@DonMag这些数据来自APIresponse@ManjuSSP没有任何东西禁止您转换和解释JSON/XML或以任何方式获取信息,对吗?我也建议先把它转换成整数。
return [finalDate2 compare:finalDate1];