简体   繁体   English

匹配NSDictionary键值对

[英]Match NSDictionary Key Value Pairs

I have the following NSDictionary with the following data structure: 我有以下具有以下数据结构的NSDictionary:

Food Count (values) - Food Name (key)

I sort that NSDictionary's food count like so: 我对NSDictionary的食物计数进行如下排序:

    NSArray *sortedFood = [[detailDictionary allValues] sortedArrayUsingComparator:^(id obj1, id obj2) {
    if ([obj1 intValue] < [obj2 intValue])
        return (NSComparisonResult)NSOrderedDescending;
    if ([obj1 intValue] > [obj2 intValue])
        return (NSComparisonResult)NSOrderedAscending;
    return (NSComparisonResult)NSOrderedSame;
}];

It has sorted it the following way 它按以下方式排序

100
98
50
30
etc.

I am trying to use that new sortedFood array to then only show the top 5 value, key pairs for the food. 我正在尝试使用该新的sortedFood数组,然后仅显示食物的前5个值,密钥对。

For example, if Apple's is the name of the food that has the value of 100, it would appear first. 例如,如果Apple的名称是值为100的食物的名称,它将首先出现。

So far I have the loop, but nothing else. 到目前为止,我已经了解了循环,但没有别的。

for (int i = 0; i <= 5; i++) {
}

Not sure what else to do. 不知道该怎么办。 I think this is more difficult because the whole dictionary isn't sorted in the same way as the new array, but I could be wrong. 我认为这比较困难,因为整个字典的排序方式与新数组不同,但是我可能是错的。

我认为您应该在NSDictionary尝试-[keysSortedByValueUsingSelector:] ,它会按值排序并获得关键结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM