Thursday, May 9, 2013

NSDictionary keysSortedByValueUsingSelector example ios


Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator
A selector that specifies the method to use to compare the values in the dictionary.
The comparator method should return NSOrderedAscending if the dictionary value is smaller than the argument, NSOrderedDescending if the dictionary value is larger than the argument, and NSOrderedSame if they are equal.
Return Value of [NSDictionary keysSortedByValueUsingSelector]
An array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.
Pairs of dictionary values are compared using the comparison method specified bycomparator; the comparator message is sent to one of the values and has as its single argument the other value from the dictionary.
Example of [NSDictionary keysSortedByValueUsingSelector]
NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil};
NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil};
NSDictionary *newScoreDict =  [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames count:7];
NSArray *currScores = [scoreDict keysSortedByValueUsingSelector:@selector(localizedStandardCompare:)];
Example of [NSDictionary keysSortedByValueUsingSelector]
NSDictionary*  dictionary = [NSDictionary dictionaryWithObjects:bothUserName forKeys:bothUID];  

NSArray *sortedKeys = [dictionary keysSortedByValueUsingSelector:@selector(compare:)];

for (NSString *key in sortedKeys) {
  NSLog(@"%@: %@", key, [dictionary objectForKey:key]);
Example of [NSDictionary keysSortedByValueUsingSelector]
- (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare
    // This sorts by product first, then fruit.
    Product *myProduct = [self objectAtIndex:0];
    Product *productToCompare = [arrayToCompare objectAtIndex:0];

    NSComparisonResult result = [myProduct.productName caseInsensitiveCompare:productToCompare.productName];
    if (result != NSOrderedSame) {
        return result;

    Fruit *myFruit = [self objectAtIndex:1];
    Fruit *fruitToCompare = [arrayToCompare objectAtIndex:1];

    return [myFruit.itemName caseInsensitiveCompare:fruitToCompare.itemName];
// Create some fruit.
Fruit *apple = [[[Fruit alloc] init] autorelease];
apple.itemName = @"apple";
Fruit *banana = [[[Fruit alloc] init] autorelease];
banana.itemName = @"banana";
Fruit *melon = [[[Fruit alloc] init] autorelease];
melon.itemName = @"melon";

// Create some products
Product *butter = [[[Product alloc] init] autorelease];
butter.productName = @"butter";
Product *pie = [[[Product alloc] init] autorelease];
pie.productName = @"pie";
Product *zinger = [[[Product alloc] init] autorelease];
zinger.productName = @"zinger";

// create the dictionary. The array has the product first, then the fruit.
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:zinger, banana, nil], @"zinger banana", [NSArray arrayWithObjects:butter, apple, nil], @"butter apple", [NSArray arrayWithObjects:pie, melon, nil], @"pie melon", nil];

NSArray *sortedKeys = [myDict keysSortedByValueUsingSelector:@selector(customCompareToArray:)];

for (id key in sortedKeys) {
    NSLog(@"key: %@", key);