views:

372

answers:

2

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.

+5  A: 

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.

Colin Barrett
I am very new to this.What should i do for my code.I have date as NSMutableString
Warrior
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: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW5
Colin Barrett
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.
@colin barrtt...>How can parse 10 date value and save it in a date format in array.Is that possible?
Warrior
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
I am using nsmutable array.What should i do for that?
Warrior
Warrior: His answer is what you should do for that.
Peter Hosey
@colin barrett....>Sorry for asking many silly question.I am very new to this iphone.So it took time to understand and implement your code in my program.Thanks I got the output.
Warrior
A: 

Take a look at NSMutableArray's sortUsingComparator: and NSComparator.

David Kanarek