4

In my project, I'm getting some data from an APi and I retreive the data into a NSMutableArray by parsing the JSON. It has a key called "StartDate" which is of the format : " mm/dd/yyyy hh:mm:ss " as shown below

StartDate: "5/18/2013 12:00:00 AM"

I'm saving these data to a resultArray .There are also 4 more keys for an object as my JSON is of the form

{
    EventId: "xxxx",
    Title: "xxx",
    Location: "xxxx",
    StartDate: "5/18/2013 12:00:00 AM",
    Link: null
}

there are multiple such objects here. All that I need to do is to sort the contents of the resultArray based on date(either ascending or descending), I use the following code

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"StartDate" ascending:TRUE];
[resultArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

But I;m getting a shuffled result, the sorting is not correct throughout, can anyone tel me where I had gone wrong .

Thanks

2
  • 1
    Maybe (probably) a dupe, but certainly not too localized. I can only suspect that the down-voters were too lazy to look up the dupes. Commented Jan 15, 2013 at 12:33
  • @HotLicks you are right! Thanks for your comment dude, i just posted so to let them be aware not to misuse their rights here! some of them see it as a hobby to downvote and flag! Commented Jan 15, 2013 at 13:02

2 Answers 2

6

Should work

[resultArray sortUsingComparator:^NSComparisonResult(id a, id b) {

    static NSDateFormatter *dateFormatter = nil;

    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"mm/dd/yyyy hh:mm:ss"; 
    }

    NSString *date1String = [a valueForKey:@"StartDate"];
    NSString *date2String = [b valueForKey:@"StartDate"];

    NSDate *date1 = [dateFormatter dateFromString:date1String];
    NSDate *date2 = [dateFormatter dateFromString:date2String];

    return [date1 compare:date2];
}];
Sign up to request clarification or add additional context in comments.

2 Comments

great answer man, i solved it using your answer, jsut changed dateformat from mm...to MM
Another option would be to rearrange the strings into yyyymmdd order. Could be done with a few simple string ops.
3

Your StartDate seems to be in the string format and I believe its shuffling it for string.. You should convert to NSDate first and than apply this descriptor... Hoping this helps.

3 Comments

Thanks for your help, but I don't have the permission to change the APi format, so should i replace each date key of my array after converting to the date format?
You must be storing it in the a data model I guess, where you can specify the property of this model object as NSDate and while feeding it to the object convert it to date... or simpler you can do what you are saying but that is not the standar way to do that... but what the heck... you should try your approach first and if it works later you can convert it to data model objects
this makes sense, but you could see more simple way by checking the accepted answer.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.