MonkeyBonkey
MonkeyBonkey

Reputation: 47851

sort NSDictionary keys by dictionary value into an NSArray

I've seen a lot of examples of sorting a dictionary by keys, then getting the values, but how would I sort instead by values.

e.g.

{
blue:12;
red:50;
white:44;
}

I would want these sorted by number descending to:

{
red:50;
white:44;
blue:12
}

I tried getting a sorted nsarray of keys from which I could created the ordered nsarray but the result still seems unordered.

  NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {

    if ( first < second ) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( first > second ) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }

  }];

Upvotes: 9

Views: 9605

Answers (3)

Baraa
Baraa

Reputation: 9

Or you could just sort your NSString keys directly like:

NSArray* sortedKeys = [sortedKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id  _Nonnull obj2) {
        NSNumber *A = @([obj1 LongValue]);
        NSNumber *B = @([obj2 LongValue]);

        return [A compare:B];
    } ];

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52227

Conceptually a NSDictionary is unsorted, as said already by C0deH4cker.

If you need an order, you can either write the keys to an array (but you might have trouble with the array retaining it after the key was removed from the dictionary, but there are tutorials how to create a un-retaining array by using the CFArray) or NSSortedSet.

Or you can subclass NSDictionary — not very trivial, as NSDictionary is a class cluster. But luckily Matt shows in his fantastic blogpost "OrderedDictionary: Subclassing a Cocoa class cluster" how to use a little trick, a covered has-a relationship.


Note, that your code

 NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {

    if ( first < second ) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( first > second ) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }

  }];

wont do, what you want it to do, as you are applying C-operators to objects. Now their pointers will be ordered.

it should be something like

 NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {
    return [first compare:second];
  }];

or if you want to order on scalars, that are wrappers as objects (ie NSNumber)

 NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) { 
    if ([first integerValue] > [second integerValue]) 
        return (NSComparisonResult)NSOrderedDescending;

    if ([first integerValue] < [second integerValue])
        return (NSComparisonResult)NSOrderedAscending;
    return (NSComparisonResult)NSOrderedSame;
}];

Upvotes: 10

C0deH4cker
C0deH4cker

Reputation: 4057

Dictionaries by definition are unordered. They are accessed using hash tables for speed. The only way that you'll be able to "order" it is to choose a different data type. The bast data type depends on your intent.

Upvotes: 0

Related Questions