I have a NSMutableArray with 30 dictionaries inside of it. Each contains a name and a value. I currently have the names sorted so that the show in a table view alphabetically. However, I'd like to make a UIButton to give the option of STILL DISPLAYING THE NAMES, but ordered by the value. The value doesn't need to be displayed. Also, where in the .m/.h files would these codes be placed? THANKS!
views:
1581answers:
1
+1
A:
Hello Nathan!
To be able to help you I need a test data to work with. Let's say your array with data looks like this:
NSMutableArray *aMutableArray = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"A", @"name",
[NSNumber numberWithInt:6], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"C", @"name",
[NSNumber numberWithInt:2], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"B", @"name",
[NSNumber numberWithInt:4], @"value", nil], nil];
In this case case here's how you sort it by value:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
As for where to place this code place it wherever you need the reordering to happen (in the implementation [.m] file of course).
HTH and if there will be any questions ask them in the comments.
Ivan Karpan
2010-01-27 18:00:50
Thank you very much! However, my array and dictionaries are stored inside a plist!!! Sorry for leaving that out. I've discovered (so it seems) that this would be extremely difficult if not impossible to accomplish. I've therefore moved onto a window-based project, with a tab bar and navigation controllers inside. I am now setting up the first tab, with a nav controller inside of it, which looks like this:
Nathan
2010-01-27 22:18:30
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. DescriptionViewController *descriptionViewController = [[DescriptionViewController alloc] initWithNibName:@"DescriptionViewController" bundle:nil]; descriptionViewController.teamarray = [self.teams objectAtIndex:indexPath.row]; [self.navigationController pushViewController:descriptionViewController animated:YES]; [descriptionViewController release];
Nathan
2010-01-27 22:19:04
However, I get the error: Object cannot be set - either readonly property or no setter found. I know it must be easy to solve, but I'm a total newbie at this... Thanks.
Nathan
2010-01-27 22:19:23
Okay, never mind. But thank you very much!
Nathan
2010-01-28 19:36:20