-2

I am just wondering if it is possible to sort a dictionary of integers in iOS? Looking at previous examples/questions it seems that most people are sorting arrays of ints or arrays of dictionaries (containing ints).

However, lets say I have a dictionary with the structure:

 {numWords = 12, numPhotos = 15, numFriends = 8}

The keys are not the same so it seems I cannot use a sort descriptor (initialising with key), so is there any way to sort the integers and keep the associated keys in-tact?

Da49
  • 107
  • 2
  • 10
  • http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary – bshirley Apr 10 '13 at 18:19
  • dictionaries don't contain `int`s, they contain objects, perhaps `NSNumber`s – bshirley Apr 10 '13 at 18:20
  • dictionaries are not ordered. How can you sort something that has no order at all? – John Apr 10 '13 at 18:24
  • possible duplicate of [Sort NSMutableDictionary keys by object](http://stackoverflow.com/q/6956277) – jscs Apr 10 '13 at 18:33
  • possible duplicate of [Sort NSMutableDictionary keys by object?](http://stackoverflow.com/questions/6956277/sort-nsmutabledictionary-keys-by-object) – gaige Apr 11 '13 at 09:50

4 Answers4

2

Dictionaries can't be sorted. But there is a method that will return an array of keys in order based on the values being sorted.

NSMutableDictionary *dict = ...; // your dictionary
NSArray *orderedKeys = [dict keysSortedByValueUsingSelector:@selector(compare:)];

In your case, orderedKeys would be numFriends, numWords, numPhotos.

To make use of the ordered keys you can do something like:

for (id key in orderedKeys) {
    id value = [dict objectForKey:key];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • But with this solution, the returned array has no pointer from each integer to each string (which in this example renders the array useless or at least confusing) – Da49 Apr 10 '13 at 18:20
  • @Da49 You can have two arrays sorted in parallel - see my answer. –  Apr 10 '13 at 18:21
  • @Da49 You can then iterate through the ordered keys and get the corresponding value. See my update. – rmaddy Apr 10 '13 at 18:22
0

NSDictionary are not ordered. And you never sort dictionary.

Indeed you sort keys and or values.

For your case:

You need to extract all the values into arrayValues parallely keys in arraykeys, sort based on arrayValues.

NSMutableArray *array1 = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:3], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:4], [NSNumber numberWithInt:6], nil];
NSMutableArray *arrayKeys = @[@"Five", @"Three", @"One", @"Two", @"Four", @"Six"];
NSMutableArray *arrayValues = @[@"Cinq", @"Trois", @"Un", @"Deux", @"Quatre", @"Six"];
 [array1 sortArrayUsingSelector:@selector(compare:) withPairedMutableArrays:arrayKeys, arrayValues, nil];

After the call to sortArrayUsingSelector:withPairedArrays:, the three arrays will all be in numeric order, so arrayKeys will look like this:

One, Two, Three, Four, Five, Six

and arrayValues will look like this:

Un, Deux, Trois, Quatre, Cinq, Six
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Assuming the objects are unique:

1) ask the dictionary for the array of allValues

2) sort the array of values

3) since there is no valueForKey, for each value, use fast enumeration or enumerateKeysAndObjectsUsingBlock: to find the appropriate key.

David H
  • 40,852
  • 12
  • 92
  • 138
0

Dictionaries are not sortable.

That said, you can have two sorted arrays, the one containing the keys and the other containing the associated values. Sort those by the ordering of the values.

NSDictionary *d = @{
    @"numWords": @12,
    @"numPhotos": @15,
    @"numFriends": @8
};

NSComparisonResult (^cmp)(id, id) = ^(id obj1, id obj2) {
    return [obj1 compare:obj2];
}

NSArray *keys = [d keysSortedByValueUsingComparator:cmp]
NSMutableArray *vals = [[id allValues] mutableCopy];
[vals sortUsingComparator:cmp];