I have to sort the following array objects in either ascending or descending order.
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3",
@"key4", @"key5", nil];
NSArray *objects = [NSArray arrayWithObjects:@"0", @"2", @"1",
@"5", @"0", nil];
NSDictionary* dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
for (id key in [dictionary allKeys]) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
My output should be like this.
key: key4, value:5
key: key2, value:2
key: key3, value:1
key: key1, value:0
key: key5, value:0
It seems you want to sort by your values. While a
NSDictionaryis not sorted, you can get an array of the keys sorted in a way you like. Just have a look atThen iterate over the keys in that array and fetch the matching value from the dictionary.