views:

27

answers:

1

I have an editable pList with a dictionary of strings in the format:

21-10-2010  ->  @"item1"
20-10-2010  ->  @"item2"
19-10-2010  ->  @"item3"
18-10-2010  ->  @"item4"
17-10-2010  ->  @"item5"
ect...

So with dates in stringformat as keys, and strings with different information as value. The lenght of the dictionary can become up to 200 keys. And the keys don't necessarily come in order, and there might be missing some dates.

I want to show the keys and their values in a tableview with UITableViewCellStyleValue2. At the moment I'm putting them in the tableview by writing all the keys to a global NSMutableAray at startup:

keysArray = [myDictionary allKeys];

and then in the tableview method, I'm putting them in by using normal indexPath as index to the array:

cell.textLabel.text = [keysArray objectAtIndex:indexPath];
cell.detailTextLabel.text = [myDictionary objectForKey: [keysArray objectAtIndex:indexPath]];

This is working, but the dates are not in order with the highest date first. This is because the 'allKeys'-function returns the keys in random order (also there arent order in the dictionary it self).

The whole method of cycling through a global array, and reffering this value as a key to another dictionary, that's loaded from a extern pList file, is really complicated and awkward. So I'm looking for either a way to sort an NSMutableArray by date, or a better way to implement what I want.

Thank you for listening, John.

A: 

You can create a array sorted by date. You will indexed this array indexPath.row to ask the item value.

To sort an array you can use:

-sortedArrayUsingComparator:

[array sortedArrayUsingComparator:^(id obj1, id obj2) { 
  // Your comparaison
}];

see this guide for block usage

Benoît
This answer put me in the right direction. I ended up doing it with a standard comparator after reading up on it.
John Kofod