简体   繁体   中英

Sorting NSArray of Dictionary containing Date as String

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;

    }];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM