开发者社区> 问答> 正文

对包含字符串形式的日期的字典的NSArray进行排序

我想根据日期参数对以下数组进行排序,但问题是来自一个服务器,我没有获得timestamp,我得到的日期是string有人能帮忙吗。

NSArray *array = @[
        @{@"valid":@"Y",@"mof":@"ON",@"dof":@"17-05-2019",@"rtntype":@"CODE1",@"ret_prd":@"042019"},
        @{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE1",@"ret_prd":@"032019"},
        @{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE2",@"ret_prd":@"032019"}
    ];

我试过应用这个解决方案,但它不能工作,因为我们的日期是在NSString而不是在NSDate或NSTimeInterval

[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

        if ([obj1 intValue] == [obj2 doubleValue])
            return NSOrderedSame;

        else if ([obj1 intValue] < [obj2 doubleValue])
            return NSOrderedAscending;

        else
            return NSOrderedDescending;

    }];

展开
收起
游客5akardh5cojhg 2019-12-16 21:24:27 560 0
1 条回答
写回答
取消 提交回答
  • 我假设您有一个特定的理由来保持数据的原样,而不是解析到模型类中,保持它的方便性。

    在您的场景中,您可以尝试使用以下代码对数组进行排序:

    NSArray *array = @[
            @{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE1",@"ret_prd":@"032019"},
            @{@"valid":@"Y",@"mof":@"ON",@"dof":@"17-05-2019",@"rtntype":@"CODE1",@"ret_prd":@"042019"},
            @{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE2",@"ret_prd":@"032019"}
        ];
    
        //NSDateFormatter to convert NSString to NSDate
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd-MM-yyyy"];
    
        NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            if ([obj1 isKindOfClass:[NSDictionary class]]
                && [obj2 isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dict1 = (NSDictionary *)obj1;
                NSDictionary *dict2 = (NSDictionary *)obj2;
                if ([dict1[@"dof"] isKindOfClass:[NSString class]]
                    && [dict2[@"dof"] isKindOfClass:[NSString class]]) {
                    NSString *dof1 = (NSString *) dict1[@"dof"];
                    NSString *dof2 = (NSString *) dict2[@"dof"];
                    NSDate *date1 = [formatter dateFromString:dof1];
                    NSDate *date2 = [formatter dateFromString:dof2];
                    return [date1 compare:date2];//Update the return based on in which order you want the resulting array
                }
            }
            return NSOrderedSame;
        }];
        NSLog(@"%@", sortedArray);
    
    

    结果是:

    (
            {
            dof = "19-04-2019";
            mof = ON;
            "ret_prd" = 032019;
            rtntype = CODE1;
            valid = Y;
        },
            {
            dof = "19-04-2019";
            mof = ON;
            "ret_prd" = 032019;
            rtntype = CODE2;
            valid = Y;
        },
            {
            dof = "17-05-2019";
            mof = ON;
            "ret_prd" = 042019;
            rtntype = CODE1;
            valid = Y;
        }
    )
    
    2019-12-16 21:25:01
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载