user564963
user564963

Reputation: 2294

sorting array of dictionaries

HI i have a following array of dictionaries and and i want to sort the array using the the key "BirthDate" and my code is as follows

NSLog(@"nodeEventArray == %@", temp);
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
                                    sortDescriptorWithKey:@"BirthDate"
                                    ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [temp
                             sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedEventArray == %@", sortedEventArray);

but I was unable to sort properly can any one please tell me how to sort the array of dictionary based on "BirthDate" key .Since birth date is coming as string from service

sample array of dictionaries:

   nodeEventArray = (
        {
        BirthDate = "05/04/1986";
        Email = "";
        FirstName = Test;
        IsSpouse = 0;
        LastName = Test;
        MiddleInitial = "";
        PatientID = "";
        Phone = "";
        ScacntronCSVId = 40409;
        SlotID = 1;
        UserName = "Tes_02192013010055";
    },
        {
        BirthDate = "02/14/1986";
        Email = "";
        FirstName = Guest;
        IsSpouse = 0;
        LastName = Guest;
        MiddleInitial = "";
        PatientID = "";
        Phone = "";
        ScacntronCSVId = 40410;
        SlotID = 2;
        UserName = "Gue_02192013014725";
    },
        {
        BirthDate = "02/17/1987";
        Email = "";
        FirstName = User;
        IsSpouse = 0;
        LastName = User;
        MiddleInitial = "";
        PatientID = "";
        Phone = "";
        ScacntronCSVId = 40411;
        SlotID = 3;
        UserName = "Use_02192013020726";
    }
    )

Upvotes: 0

Views: 309

Answers (2)

Sunil Pandey
Sunil Pandey

Reputation: 7102

Instead of using sortDescriptor you can use comparator

NSArray *sortedArray = [nodeEventArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
        NSDateFormatter *dateFormator = [[NSDateFormatter alloc] init];
        NSDate *date1 = [dateFormator dateFromString:[[obj1 objectForKey:@"BirthDate"] stringByReplacingOccurrencesOfString:[[obj1 objectForKey:@"BirthDate"] lastPathComponent] withString:@"2013"]];
        NSDate *date2 = [dateFormator dateFromString:[[obj1 objectForKey:@"BirthDate"] stringByReplacingOccurrencesOfString:[[obj1 objectForKey:@"BirthDate"] lastPathComponent] withString:@"2013"]];
        if ([date1 earlierDate:date2] == date1) {
            return (NSComparisonResult)NSOrderedDescending;
        }else{
            return (NSComparisonResult)NSOrderedAscending;
        }

    }];

Upvotes: 0

Fogmeister
Fogmeister

Reputation: 77651

I assume the array was coming from a server or something (hence the "Username" bit) but still, it would be better to store each user/event/whatever in an object with an NSDate. That way you can just sort by the date and it works.

However, if you want to sort this array then...

If you want to sort by BirthDate (using the date) then you'll need to convert the strings into actual NSDates.

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"MM/dd/yyyy";

[blah sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    NSDate *date1 = [df dateFromString:[obj1 objectForKey:@"BirthDate"];
    NSDate *date2 = [df dateFromString:[obj2 objectForKey:@"BirthDate"];

    return [date1 compare:date2];
}];

This should do it.

It will convert the strings into dates and then compare them.

Upvotes: 1

Related Questions