0

I want to do sorting for NSMutableDictionary values based on no of players?

Also i want to know how can i append static content with diffrent value like below

myDictionary = [NSMutableDictionary dictionary];

[myDictionary setValue:[NSNumber numberWithInt:10] forKey:@"no_of_players"];
[myDictionary setValue:[NSNumber numberWithInt:3] forKey:@"difficulty_level"];
[myDictionary setValue:[NSNumber numberWithInt:120]] forKey:@"duration_excercise"];
[myDictionary setValue:[NSNumber numberWithInt:1] forKey:@"Excercise_id"];

Thanks for any help

user198725878
  • 6,266
  • 18
  • 77
  • 135
  • hi..i want to add another set of values in it..i ma not sure how to do..any helps pls – user198725878 May 10 '11 at 10:47
  • Are you sing instances of `NSMutableDictionary` as domain objects? In this case you should seriously consider creating a proper domain class. So that you do not have to convert to and from `NSNumber` instances, and you can also implement a custom `compare:` method for that domain object that you can use for sorting. – PeyloW May 10 '11 at 13:50

3 Answers3

1

You have answered the part of question where you append static content in the question itself. Regarding sorting check the link below:

Sort an NSMutableDictionary

Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69
1

NSDictionary are unsorted by nature. The order of the objects as retrieved by allKeys and allValues will always be undetermined. Even if you reverse engineer the order it may still change in the next system update.

There is however more powerful alternatives to allKeys that are used to retrieve the keys in a defined and predictable order:

  • keysSortedByValueUsingSelector: - Useful for sorting in ascending order according to the compare: method of the value objects.
  • keysSortedByValueUsingComparator: - New in iOS 4, use a block to do the sort inline.
visakh7
  • 26,380
  • 8
  • 55
  • 69
1

use this for sorting, your array contains nsmutabledictionaries then you can sort by this

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"no_of_players" ascending:YES];
    NSArray *arr=[NSArray arrayWithObject:sort];
    [yourArray sortUsingDescriptors:arrr];
Ishu
  • 12,797
  • 5
  • 35
  • 51