How to sort month name in ascending or descending order using NSSortDescriptor in objective c

邮差的信 提交于 2019-12-13 08:29:41

问题


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:


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.



来源:https://stackoverflow.com/questions/48322519/how-to-sort-month-name-in-ascending-or-descending-order-using-nssortdescriptor-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!