note from N00B land again. I have read lots about sorting arrays - wanted to try the block method, but haven't wrapped my head around it. Instead, I opted for the descriptors method. I read this Sort NSArray of custom objects by their NSDate properties and this How to sort an NSMutableArray with custom objects in it? amongst oodles and oodles of others. In my code I did this:
NSString *lastHighScore = @"_highScore";
NSString *dateScoreCreated = @"_dateCreated";
NSSortDescriptor *highScoreDescriptor = [[[NSSortDescriptor alloc]
initWithKey:lastHighScore
ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSSortDescriptor *dateScoreCreatedDescriptor = [[[NSSortDescriptor alloc]
initWithKey:dateScoreCreated
ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *descriptors = [NSArray arrayWithObjects:highScoreDescriptor,
dateScoreCreatedDescriptor, nil];
NSArray *sortedArray = [[[FlipHighScoreStore sharedStore] allHighScores] sortedArrayUsingDescriptors:descriptors];
Sadly, I am getting an error to begin with - initializer element is not a compile-time element. I looked this up and tried setting NSSortDescriptor *highScoreDescriptor = nil
but then I get a warning saying that highScoreDescriptor "Type Specifier Missing, default to int" which in this case is ok, but is not so ok for the Date object in the next descriptor. (Turns out I am also getting an error saying that I am redefining highSoreDescriptor with a different type.)
Also, is there a list somewhere of what selectors are available? I doubt that localizedCaseInsensitiveCompare:
is what I want to use since the first property "_highScore" is an int and the second "_dateCreated" is a date. I read somewhere that the default is "compare" so can I just put "compare:"? (Found one answer, I think - I can use (intValue) for the first descriptor:
selector:@selector(intValue)] autorelease];
More reading makes me think that I can do away with the selector line entirely for the date sort. Is that correct?
Lastly, if I say ascending:NO
is that the same as descending? I would guess that it is, but one never knows with programming, does one?
Do I wrap all of this code in its own method? Or can I (until later) just plunk it in the code where I am laying out the table?
This project is not ARC.