How to sort an array of dates in descending order

baste picture baste · Jan 26, 2013 · Viewed 28.5k times · Source

I have a NSDictionary that parsed to an array one of the element is date, i tried using [startimeArray sortUsingSelector:@selector(compare:)]; (starttimeArray) is my date but it only arrange ascending. how can i sort in descending order. thanks

Answer

Anoop Vaidya picture Anoop Vaidya · Jan 26, 2013

Sort the array by puting NO is ascending parameter:

NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[dateArray sortedArrayUsingDescriptors:descriptors];