suse
suse

Reputation: 10563

Sort Date[dd/MM/yyyy] which is in NSString data type inside an array in Objective-C for iPhone

I have an NSMutableArray which holds the dates in format dd/MM/yyyy of type NSString. Now i need to display the sorted array of dates. Please suggest me how to achieve it.

Thank You.

Upvotes: 1

Views: 502

Answers (1)

Swapnil Luktuke
Swapnil Luktuke

Reputation: 10475

I can think of two options:

  1. You can use NSMutableArray's sortUsingFunction or sortUsingSelector etc. methods. Create the sort method or function which takes string parameters (array object's datatype), converts the strings to dates using NSDateFormatter's dateFromString method, compares the two dates using NSDate's compare method and return the comparison result. For a typical example of the sort function for an array, see NSArray class reference - sortedArrayUsingFunction.

  2. Implement any of typical sort algorithms (like quick sort) yourself. In the implementation convert the date to string before you compare two elements.

Upvotes: 2

Related Questions