IPhone Obj C - Sort Mutable Array of dictionaries - display a string, but sort by value

I have an NSMutableArray with 30 dictionaries inside it. Each of them contains a name and a meaning. I currently have sorted names to display in a table in alphabetical order. However, I would like to make UIButton to enable STILL DISPLAYING THE NAMES, but in order of value. The value does not need to be displayed. Also, where will these codes be placed in the .m / .h files? THANKS!

+3
source share
1 answer

To help you, I need test data to work. Let say that your data array looks as follows:

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, you can sort it by value:

NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];

As for the location of this code, wherever you need reordering (in the implementation file [.m], of course).

HTH, and if you have any questions, ask them in the comments.

+22
source

Source: https://habr.com/ru/post/1730254/


All Articles