4

(Prepare to witness a newbie being very, very confused at what I'd assume is a basic form of logic that my brain is struggling to grasp.)

I have a .plist file at the moment. In the "Key" column there are names of events (it's just dummy content at the moment), and in the "Value" column there are dates in this format: 19-07-2012. Each row is of the "string" type.

In the viewDidLoad method, I use the following code:

NSString *theFile = [[NSBundle mainBundle] pathForResource:@"dates" ofType:@"plist"];
theDates = [[NSDictionary alloc] initWithContentsOfFile:theFile];
theDatesList = [theDates allKeys];

This loads the plist file into the dictionary, then I load the keys into an array, which is the way I've learned to populate a UITableView, specifically with this code in the cellForRowAtIndexPath method:

NSString *eventFromFile = [theDatesList objectAtIndex:indexPath.row];
NSString *dateFromFile = [theDates objectForKey:[theDatesList objectAtIndex:indexPath.row]];

But what I'm confused about now is, how do I order the cells of the UITableView based on what dates are the soonest? So, the cell for the 19th of July would appear before the 21st of August, no matter what order it's in within the plist file.

Within the UITableViewCell I've managed to calculate the number of days between the current date and the date defined within the plist. That's this code:

// Set the date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];

// Get the current, then future date
NSDate *currentDate = [NSDate date];
NSDate *futureDate = [dateFormatter dateFromString:dateFromFile];

// Create the calendar object
NSCalendar *theCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Extract the "day" component from the calendar object
NSDateComponents *theDifferenceBetweenDays = [theCalendar   components:NSDayCalendarUnit
                                                            fromDate:currentDate
                                                            toDate:futureDate
                                                            options:0];

NSInteger theRemainingDays = [theDifferenceBetweenDays day];

But I really have no idea what I'm doing. Could someone give me a nudge in the right direction? I've looked into NSSortDescriptors and the sortedArrayUsingSelector method, which seem to be relevant, but the act of actual implementation has left me stuck for the past six hours. Or maybe they're not the right track. Like I said, I'm quite confused.

Thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
user1191304
  • 59
  • 1
  • 3
  • The solution is to extract your plist into an array of dictionaries, then to sort the array. I'm not at the mac ATM so don't feel up to writing a code sample - if nobody's filled in the blanks when I get back, I'll put a full answer in. – jrturton May 20 '12 at 08:52
  • In fact, see here: http://stackoverflow.com/questions/4824573/how-sorting-array-which-contains-dictionary – jrturton May 20 '12 at 08:56
  • Thanks for the quick description. That code sample would be awesome, though. This is only the second iOS app I've ever tried to make (and the first one was much, much, much simpler), so I'm quite lost. :) – user1191304 May 20 '12 at 09:14

2 Answers2

1

There are several ways to sort an NSArray of NSDates in descending order.

You could use sortedArrayUsingDescriptors::

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
theDatesList = [theDatesList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

But I personally prefer sortedArrayUsingComparator::

theDatesList = [theDatesList sortedArrayUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2){
    return [date2 compare:date1];
}];
Tomas Camin
  • 9,996
  • 2
  • 43
  • 62
-2

There exists a objective C equvialent for monotouch.dialog. with this, it should be really easy.

Ploetzeneder
  • 1,281
  • 4
  • 20
  • 34