user198725878
user198725878

Reputation: 6386

Help me on NSMutableDictionary

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

Upvotes: 0

Views: 281

Answers (3)

Ishu
Ishu

Reputation: 12787

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];

Upvotes: 1

visakh7
visakh7

Reputation: 26390

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.

Upvotes: 1

Praveen S
Praveen S

Reputation: 10393

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

Upvotes: 1

Related Questions