0

I have the Following NSArray that has a NSDictionary and I need to order them so that the higher dictionary value goes on top.

I've researched around but I can not find a predicate to sort my NSDictionary. How could I achieve my objective??

 NSArray* result = @[@{ @"chest":[NSString stringWithFormat:@"%i",[chestR count]]},
                      @{@"back":[NSString stringWithFormat:@"%i",[backR count]]},
                      @{@"triceps":[NSString stringWithFormat:@"%i",[tricepR count]]},
                      @{@"biceps":[NSString stringWithFormat:@"%i",[bicepR count]]},
                      @{@"arms-other":[NSString stringWithFormat:@"%i",[armsR count]]},
                      @{@"legs":[NSString stringWithFormat:@"%i",[legsR count]]},
                      @{@"shoulders":[NSString stringWithFormat:@"%i",[shouldersR count]]},
                      @{@"abs":[NSString stringWithFormat:@"%i",[absR count]]},
                      @{@"cardio":[NSString stringWithFormat:@"%i",[cardioR count]]},
                      @{@"fitness":[NSString stringWithFormat:@"%i",[fitnessR count]]}
                        ];

EDIT AND SOLUTION

This is how I solved my problem

NSArray* result = @[@{ @"chest":@"5"},
                        @{@"back":@"3"},
                        @{@"triceps":@"4"},
                        @{@"biceps":@"9"},
                        @{@"arms-other":@"1"},
                        @{@"legs":@"0"},
                        @{@"shoulders":@"2"},
                        @{@"abs":@"3"},
                        @{@"cardio":@"8"},
                        @{@"fitness":@"9"}
                        ];

    NSArray *sorted = [result sortedArrayUsingComparator:^(id obj1, id obj2){

        NSArray *s1k = [obj1 allKeys];
        NSInteger s1 = [obj1[[s1k objectAtIndex:0]] intValue];
        NSArray *s2k = [obj2 allKeys];
        NSInteger s2 = [obj2[[s2k objectAtIndex:0]] intValue];

        if ( s1 > s2) {
            return (NSComparisonResult)NSOrderedAscending;
        } else if (s1 < s2) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        return (NSComparisonResult)NSOrderedSame;
    }];
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • `-allValues` and a sort descriptor should do nicely. – CodaFi Jul 23 '13 at 21:10
  • http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary – Jake Spencer Jul 23 '13 at 21:10
  • Dictionaries can't be sorted. You can the array of values or the array of keys, but not the dictionary itself. – rmaddy Jul 23 '13 at 21:14
  • An NSDictionary has no defined order. There's no guarantee that you can even dump it twice in a row and have the values dump in the same order. – Hot Licks Jul 23 '13 at 21:14
  • You can check this posts: http://stackoverflow.com/questions/9708742/getting-nsdictionary-keys-sorted-by-their-respective-values ------------------------------ http://stackoverflow.com/questions/3583020/can-i-sort-the-nsdictionary-on-basis-of-key-in-objective-c – Goca Jul 23 '13 at 21:25
  • What do you mean by "the highest dictionary value"? How would you compare them? – Ramy Al Zuhouri Jul 23 '13 at 21:34
  • Basically I need that the highest `muscle group` `value` is the first element on the array. but I need need to keep the `muscle group`:`value` reference Dictionary. I just need to reorder the array to match that criteria – Jonathan Thurft Jul 23 '13 at 21:36
  • @JonathanThurft Instead of editing the answer, answer to your own question and accept it. – Ramy Al Zuhouri Jul 23 '13 at 22:36

1 Answers1

0

A dictionary is not sorted - by definition.

You may convert it to an array. The NSDictionary instance method -allValues may be suitable for doing so.

Then you can sort the new array by using a custom sort descriptor which ensures that it is sorted in exactly the way you want it to be.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • Could you please provide some example on how to do the sorting please? – Jonathan Thurft Jul 23 '13 at 21:20
  • Yes, I could. However, it is close to midnight here and I may not be in the best condition to doing so. :-) You will find a good example in the answer to this question. http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it I have used the same question and answer when I had the deed for a custom sort descriptior (Just without the dictionary aspect at that time). – Hermann Klecker Jul 23 '13 at 21:23
  • Do you need the sorted result to be in some dictionary later (after sorting) or is your question really just focussed on sorting the values which just happen to be in a dictionary? – Hermann Klecker Jul 23 '13 at 21:25
  • I need the results in a dictionary with the same key & value matching. Just order them in the array by the value of the dictionary. – Jonathan Thurft Jul 23 '13 at 21:29
  • Hmm... you can't. You could copy the values to an array, as discussed, and sort it. And you could buld up a dictionay where you use the values as keys, this time, and the keys as values. There are several ways of switching keys and values in a dict. But in the end a dictionary as such cannot be sorted. Well, you could implement you own enumerator but that would just add additional complexity (but may save memory though) which considers your sorting criteria. But frankly, a sorted array on one hand and a dictionary with reverted keys and values shoud serve your purpose. – Hermann Klecker Jul 23 '13 at 21:34