0

I have an unordered NSMutableDictioanry and this dictioanry is created as in following example

{
    1 = 27;
    2 = 5;
    3 = 13964;
    4 = 2422;
    5 = 45;
    6 = 7;
    7 = 27;
    8 = 39;
}

i want to sort this dictionary based on values. And this can be done according to following article and it works perfectly fine Getting NSDictionary keys sorted by their respective values and will returns an array with keys sorted based on values

(
    2,
    6,
    7,
    1,
    8,
    5,
    4,
    3
)

So my question is there anyway that i can get a sorted dictionary directly instead of array

Community
  • 1
  • 1
Mr.G
  • 1,275
  • 1
  • 18
  • 48
  • What do you want to do with the sorted dictionary? – Jeffery Thomas Sep 27 '16 at 09:47
  • NSDictionary don't care about sorting their keys, because it's a key access system and not an index access system to get the values. – Larme Sep 27 '16 at 09:48
  • Possible duplicate of [Sort Dictionary by values in Swift](http://stackoverflow.com/questions/24090016/sort-dictionary-by-values-in-swift) – Bista Sep 27 '16 at 09:48
  • the _dictionary_ is assorted like a _set_, you need to create an _array_ and put into that what you'd like to sort (e.g. keys). – holex Sep 27 '16 at 09:49
  • Dictionaries are unsorted collections by design. So, maybe not the right choice in your case... – Alladinian Sep 27 '16 at 09:49
  • i was trying to send the each dictionary values to table cell , so will need both key and value (they will use in cell) – Mr.G Sep 27 '16 at 09:50
  • @Mr.G, then you could sort the keys e.g. into an _array_ based on the _value_ then restore back the _value_ from the _dictionary_ by the _array_'s item (=_key_) for the actual index path. – holex Sep 27 '16 at 09:53
  • yeah i was gonna do that , but i thought mutable dictionary would have a rich method to sort the dictioary – Mr.G Sep 27 '16 at 09:54
  • the _dictionary_'s keys are not sorted at all. what exactly would they be sorted by? – holex Sep 27 '16 at 09:56
  • Possible duplicate of [Sort an NSMutableDictionary](http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary) – Guillaume Algis Sep 27 '16 at 09:59

2 Answers2

0

I'm not a big fan of parallel objects, but you might want to have a sorted array of keys and a dictionary.

Early on, when the dictionary has been set (maybe in -viewWillAppear:).

self.sortedKeys = [self.dictionary keysSortedByValueUsingSelector:@selector(compare:)];

Then in -tableView:cellForRowAtIndexPath: use

NSNumber *key = self.sortedKeys[indexPath.row];
NSNumber *value = self.dictionary[key];
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0

As a non-parallel object solution, you could use an array to store the key-value pairs as an array of two items. keyValuePair[0] is the key and keyValuePair[1] is the value.

- (NSArray *)sortedDataFromDictionary:(NSDictionary *)dictionary {
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:dictionary.count];

    for (NSNumber *key in [dictionary keysSortedByValueUsingSelector:@selector(compare:)]) {
        NSArray *keyValuePair = @[key, dictionary[key]];
        [result addObject:keyValuePair];
    }

    return [result copy];
}

Early on, when the dictionary has been set (maybe in -viewWillAppear:).

self.sortedData = [self sortedDataFromDictionary:dictionary];

Then in -tableView:cellForRowAtIndexPath: use

NSNumber *key = self.sortedData[indexPath.row][0];
NSNumber *value = self.sortedData[indexPath.row][1];
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117