Sorting NSArray of Dictionary containing Date as String

我是研究僧i 提交于 2019-12-25 17:24:48

问题


I want to sort the following array based on the Date parameter but the problem is from a server I am not getting a timestamp, I am getting the date as a string, can anyone please help.

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

I have tried applying the solution but it won't work as the Date we have is in NSString and not in NSDate or 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;

    }];

回答1:


I am assuming you have a specific reason to keep data as it is instead of parsing into model class keep it handy.

In your scenario you could try the following code to sort the array:

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

And the result is:

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


来源:https://stackoverflow.com/questions/59355284/sorting-nsarray-of-dictionary-containing-date-as-string

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