Ios 如何使用字典按值排序NSarray

Ios 如何使用字典按值排序NSarray,ios,objective-c,cocoa-touch,nsarray,nsdictionary,Ios,Objective C,Cocoa Touch,Nsarray,Nsdictionary,我有下面的NSArray,它有一个NSDictionary,我需要对它们进行排序,以便将较高的dictionary值放在顶部 我已经研究过了,但是我找不到一个谓词来对我的字典进行排序。我怎样才能实现我的目标 NSArray* result = @[@{ @"chest":[NSString stringWithFormat:@"%i",[chestR count]]}, @{@"back":[NSString stringWithFormat:@"%

我有下面的
NSArray
,它有一个
NSDictionary
,我需要对它们进行排序,以便将较高的dictionary值放在顶部

我已经研究过了,但是我找不到一个谓词来对我的字典进行排序。我怎样才能实现我的目标

 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]]}
                        ];
编辑和解决方案

这就是我解决问题的方法

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;
    }];
NSArray*result=@[{@“胸部”:@“5”},
@{@“后退”:@“3”},
@{“三头肌”:“4”},
@{“二头肌”:“9”},
@{“其他武器”:“1”},
@{@“legs”:@“0”},
@{@“肩膀”:@“2”},
@{@“abs”:@“3”},
@{@“心脏”:“8”},
@{@“适合度”:@“9”}
];
NSArray*排序=[结果排序使用比较器进行排序:^(id obj1,id obj2){
NSArray*s1k=[obj1所有键];
NSInteger s1=[obj1[[s1k objectAtIndex:0]]intValue];
NSArray*s2k=[obj2所有键];
NSInteger s2=[obj2[[s2k objectAtIndex:0]]intValue];
如果(s1>s2){
返回(NSComparisonResult)或删除;
}否则如果(s1
字典不按定义排序

您可以将其转换为数组。NSDictionary实例方法-allValues可能适合这样做


然后,您可以使用自定义排序描述符对新数组进行排序,以确保它按照您希望的方式进行排序

-allValues
和排序描述符应该很好。字典无法排序。您可以选择值数组或键数组,但不能选择字典本身。NSDictionary没有定义的顺序。无法保证您可以连续转储两次,并以相同的顺序转储值。您可以查看以下帖子:------------------------------------您可以提供一些如何进行排序的示例吗?是的,我可以。然而,现在已经接近午夜了,我的状态可能不是最好的你会在这个问题的答案中找到一个很好的例子。当我有自定义排序描述符的契约时,我使用了相同的问题和答案(当时没有字典方面)。您需要排序结果在以后(排序后)出现在某个字典中吗或者你的问题真的只是集中在对恰好出现在字典中的值进行排序?我需要一个具有相同键和值匹配的字典中的结果。只需按照字典的值在数组中排序即可。嗯。。。你不能。如前所述,您可以将值复制到数组中,并对其进行排序。你可以建立一个词汇表,把值作为键,这一次,把键作为值。在dict中有几种切换键和值的方法,但最终字典本身无法排序。当然,您可以实现自己的枚举器,但这只会增加额外的复杂性(但可能会节省内存),这会考虑您的排序条件。但坦率地说,一方面,一个排序数组和一个带有还原键和值的字典应该可以满足您的需要。