Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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!

share|improve this question

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
Do you mean: [myMutableArray addObject:date]; [myMutableArray sortUsingSelector:(compare:)]; ? – Magnus Jun 3 '11 at 14:30
    
@Magnus :Yes, you r right – Jhaliya Jun 3 '11 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.