1

I have an NSMutableArray of Strings. The Strings in the array, are converted from dates to Strings and I need to sort them and show them in a tableview.

I need:

June 24, 2011 June 26, 2011 July 1, 2011

And so on.

I know questions similar to this have been asked, but I didn´t get it to work. So I would really appreciate if someone could help me!

3 Answers 3

4

Sort them while they're still dates, keep them as a sorted list of dates, and only format them as text as needed for display (i.e., during -tableView:cellForIndexPath:).

Alternatively, the ISO 8601 date formats (an example formatted date would be 20110603T1345.123-4) mostly sort lexicographically the same as they would as dates. Times in different time zones or that cross a summer time shift can invalidate this property, though, so leaving dates as dates would still be your best bet.

Sign up to request clarification or add additional context in comments.

Comments

0

Easy way to do this is to create a function of the form

NSInteger funcName(id left, id right, void* extra);

you can then use NSMutableArray's sortUsingFunction:context: passing your comparison function in. Jeremy is correct in stating that dates are easier to sort, they have built in comparison functions:

– isEqualToDate:
– earlierDate:
– laterDate:
– compare:

See NSMutableArray for sorting information and NSDate for its comparison methods

Comments

0

Your NSMuatbleArray must have the NSDate object.

First you need to sort the array using -[NSMutableArray sortUsingSelector:] and need to pass @selector(compare:).

The compare: function of NSDate will do the job for you and sort the array in ascending order ..

After following the above you just need to call reloadData function of UITableView.

1 Comment

Do you mean: [myMutableArray addObject:date]; [myMutableArray sortUsingSelector:(compare:)]; ?

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.