sort NSDictionary keys by dictionary value into an NSArray

前端 未结 3 1696
情深已故
情深已故 2021-01-02 13:43

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         


        
3条回答
  •  盖世英雄少女心
    2021-01-02 14:18

    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];
        } ];
    

提交回复
热议问题