How to sort month name in ascending or descending

2019-09-20 01:33发布

i need to sort the month and year in ascending or descending order using NSSortDescriptor i have one array

{
month = Dec;
year = 2017;
},
{
month = Oct;
year = 2017;
},
{
month = Jan;
year = 2018;
}

i used to done this code

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]];
    }

i need to sort data is Feb 2017 Mar 2017 June 2017 August 2017 January 2018

1条回答
唯我独甜
2楼-- · 2019-09-20 02:04
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);

Output:

$>Sorted: (
        {
        month = Oct;
        year = 2017;
    },
        {
        month = Dec;
        year = 2017;
    },
        {
        month = Jan;
        year = 2018;
    }
)

So what's the logic?
You can't do that with a simple NSSortDescriptor, because your month is in letter, and "Dec" is before "Oct" alphabetically order speaking, but not in "time" speaking. So you need to create a NSDate that can be simply compared. You can save that date in the dictionaries of your array if needed, or use a sortedArrayUsingComparator: with a block.

To reverse the order, the easiest way in the block:

return [finalDate1 compare:finalDate2];

or

return [finalDate2 compare:finalDate1];

Note: The way of constructing finalDate1/finalDate2 might be not optimum.

查看更多
登录 后发表回答