Take the 2-minute tour ×

I am new to iphone development .I have mutable array recent in which the values such as title,date,url and summary are stored as " object for key".The keys are myurl,mudate,mytitle,mysummary.I want to sort that mutable array in reference to date and print all the values according to that..How can i achieve that?Please help me out.Thanks.

            [item setObject:currentTitle forKey:@"title"];
            [item setObject:currentLink forKey:@"link"];
            [item setObject:currentSummary forKey:@"description"];
            [item setObject:currentDate forKey:@"pubDate"];//
            [item setObject:currentImage forKey:@"imagePath"];

            [recent addObject:[item copy]];

All are xml parsed contents.

share|improve this question
Did you mean mutable dictionary? –  Giao Feb 19 '10 at 18:18
values as mutable dictionary in mutable array –  Warrior Feb 19 '10 at 18:19
Could you post a snippet of code showing how you create you array? Otherwise its hard to see how your data is structured –  pheelicks Feb 19 '10 at 18:19
[recent addObject:[item copy]]; ...> recent is mutable array ,item is mutable dictionary –  Warrior Feb 19 '10 at 18:21
Warrior: You're leaking the immutable dictionary you put into the mutable array. Review the memory management rules: developer.apple.com/mac/library/documentation/General/… –  Peter Hosey Feb 20 '10 at 6:56
add comment

2 Answers

up vote 6 down vote accepted

So I'm guessing you have an array of dictionaries. There are a couple ways to do this, but the one I recommend is using NSSortDescriptor. You'll want to do something like this:

//myArray is the name of the your array
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"mydate" ascending:YES]; // If you want newest at the top, pass NO instead.
[myArray sortUsingDescriptor:descriptor]; // This will modify myArray. To return a new array and leave the original untouched, use -sortedArrayUsingDescriptor:.
[descriptor release];
NSLog(@"%@", myArray); // Print.

If you you have your dates as NSDate objects already, this will work fine. If not, you'll either need to loop through the array and parse the dates beforehand, or use the initWithKey:ascending:selector: method of NSSortDescriptor, which allows you to define a custom comparison method.

share|improve this answer
I am very new to this.What should i do for my code.I have date as NSMutableString –  Warrior Feb 19 '10 at 18:28
I would recommend changing your code to parse the date into an NSDate at the place where its added to the array. How you do that depends on what format the date is in. In general though, you'll want to create an NSDateFormatter and then use the setDateFormat: method to tell it how to parse your dates. Then call its dateFromString: method. For more info, see: developer.apple.com/mac/library/documentation/cocoa/conceptual/… –  Colin Barrett Feb 19 '10 at 18:34
You can use the NSDateFormatter like that: NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; NSDate *myDate = [df dateFromString: myDateAsAStringValue]; B@Colin Barrett I don't known this method with the NSSortDescripor. It's a very good solution. Thank you. –  Yannick L. Feb 19 '10 at 18:40
@colin barrtt...>How can parse 10 date value and save it in a date format in array.Is that possible? –  Warrior Feb 19 '10 at 18:49
Warrior: It depends on what format your date is in. If your date looks like "1977-05-25" then you want yyyy-MM-dd. Read the documentation I linked to above for more of the format, or ask another question on SO. –  Colin Barrett Feb 19 '10 at 19:00
show 3 more comments

Your Answer


