0

I made a little program calculating gas consumption. There is one view controller for each data entry necessary to calculate consumption in the final view controller. So you enter the driven distance in the first controller, the gas fuelled up in the next controller. in the controller displaying the result, the current date is added, too. All entries are stored in a plist file as strings for easier use with the tableviews later (except for the date).

Now I want to open this data in a summary view controller to populate a tableview.

Currently I have retrieve 3 arrays from the plist file (similar to the columns of a table): array 1: NSDate array 2: NSString representing a number array 3: NSString representing a number

I know how to populate my tableview, but I cannot wrap my head around the sorting. I tried changing the column approach into a table row based approach getting every single element from each of the arrays and adding them to a new array, containg each a date and two numbers (as strings).

I tried setting up sortdescriptors, I tried using selectors, but it seems I don't fully comprehend how to work it. Personally, I prefer examples, so I was looking for tutorials on this topic. The developer documentary didn't help that much.

I don't expect to get a complete example for this problem, but maybe some pointers on how to arrange my data and what best to use to sort the complete data array.

Thanks for any pointers or examples.

EDIT: I finally got all "Table-lines" as "rows" into a dictionary. Now I can access the single columns by key for the column. And this also allows sorting the stringvalues in my array of dictionaries ascending or descending using a sortdescriptor.

This may not be the most elegant approach, but I think I now can do what I wanted. Excuse the "Table" comparisons, but I find this easier to grasp.

If anyone has pointers to good array tutorials, I'd be more than thankful.

Mike73
  • 51
  • 1
  • 6

1 Answers1

2

I'm not really clear on how you want to sort, but maybe you can adapt something from this (sorts an array of dictionaries each with a key @"row" with a value type NSNumber) -

First of all, make a comparison result function:

static NSComparisonResult compareRowNumbers(NSDictionary *dict1, NSDictionary *dict2, void * context) {

    if ([[dict1 objectForKey:@"row"] intValue] < [[dict2 objectForKey:@"row"] intValue]) {return NSOrderedAscending;}
    if ([[dict1 objectForKey:@"row"] intValue] > [[dict2 objectForKey:@"row"] intValue]) {return NSOrderedDescending;}

    return NSOrderedSame;

}

Then you can use it like so -

NSMutableArray *myArray = [NSMutableArray array];

// populate your array with loads of stuff (in this case, NSDictionaries as decribed above)
// now sort it

[myArray sortUsingFunction:compareRowNumbers context:NULL];

...and your array is automagically sorted.

SomaMan
  • 4,127
  • 1
  • 34
  • 45
  • I think I could have used your answer to actually sort my array for date. But I found out the hard way how to do it, and finally found exactly what I found out before here:http://stackoverflow.com/questions/7507457/sort-array-of-dictionaries-by-nsdate So maybe by reading into the link, it's getting clearer what I needed. Excuse my possibly bad explanation before, but I started programming from scratch by books last October and I'm still learning. I'll try your answer the next time I need to compare single elements with each other. But Thank you. – Mike73 Apr 17 '12 at 15:44