如何在目标c中使用NSSortDescriptor按升序或降序对月份名称进行排序 [英] How to sort month name in ascending or descending order using NSSortDescriptor in objective c

查看:78
本文介绍了如何在目标c中使用NSSortDescriptor按升序或降序对月份名称进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我曾经做过这个代码

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 月

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

推荐答案

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

那么逻辑是什么?
你不能用一个简单的 NSSortDescriptor 来做到这一点,因为你的月份是字母,而Dec"在Oct"之前按字母顺序排列,而不是时间".所以你需要创建一个可以简单比较的NSDate.如果需要,您可以将该日期保存在数组的字典中,或者使用带有块的 sortedArrayUsingComparator:.

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

return [finalDate2 compare:finalDate1];

注意:finalDate1/finalDate2 的构造方式可能不是最优的.

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

这篇关于如何在目标c中使用NSSortDescriptor按升序或降序对月份名称进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆