Please note, this blog entry is from a previous course. You might want to check out the current one.
Include the ability to sort the scores shown in the Extra Credit above by last played, score or game duration.
Adjust the layout and add three buttons:
Create actions for the buttons which sort the results array using the sorting methods from last task:
- (IBAction)sortByDate { self.scores = [self.scores sortedArrayUsingSelector:@selector(compareDate:)]; [self updateUI]; } - (IBAction)sortByScore { self.scores = [self.scores sortedArrayUsingSelector:@selector(compareScore:)]; [self updateUI]; } - (IBAction)sortByDuration { self.scores = [self.scores sortedArrayUsingSelector:@selector(compareDuration:)]; [self updateUI]; }
The first one is new (comparing dates) and needs to be added to the model (don’t forget to make it publicly available in the header file, too):
- (NSComparisonResult)compareDate:(GameResult *)result { return [self.end compare:result.end]; }
Note the sorting methods sort in ascending order, if you want the other way around, you need to adjust them 😉
The complete code is available on github.
reverse the order of obj1 and obj2 in [(NSDate *) obj1 compare:(NSDate *) obj2] to make it descending.
Agreed 😉