I want to do sorting for NSMutableDictionary values based on no of players?
Also i want to know开发者_如何学C 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
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];
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
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 thecompare:
method of the value objects.keysSortedByValueUsingComparator:
- New in iOS 4, use a block to do the sort inline.
精彩评论