按字典值将NSDictionary键排序到NSArray中 [英] sort NSDictionary keys by dictionary value into an NSArray

查看:99
本文介绍了按字典值将NSDictionary键排序到NSArray中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多按键排序字典的例子,然后获取值,但我如何按值排序。

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.

例如

{
blue:12;
red:50;
white:44;
}

我希望这些按数字降序排列为:

I would want these sorted by number descending to:

{
red:50;
white:44;
blue:12
}

我试过从中获取一个排序的密钥nsarray我可以创建有序的nsarray但结果似乎仍然无序。

I tried getting a sorted nsarray of keys from which I could created the ordered nsarray but the result still seems unordered.

  NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {

    if ( first < second ) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( first > second ) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }

  }];


推荐答案

从概念上讲,NSDictionary是未分类的,正如C0deH4cker已经说过的那样。

Conceptually a NSDictionary is unsorted, as said already by C0deH4cker.

如果您需要订单,可以将密钥写入数组(但是在从字典中删除密钥后,您可能会遇到阻止它的数组的问题,但有教程如何使用CFArray创建非保留阵列)或 NSSortedSet

If you need an order, you can either write the keys to an array (but you might have trouble with the array retaining it after the key was removed from the dictionary, but there are tutorials how to create a un-retaining array by using the CFArray) or NSSortedSet.

或者你可以继承NSDictionary - 不是很简单,因为NSDictionary是一个类集群。但幸运的是,Matt在他的精彩博文中显示了OrderedDictionary:Subclassing Cocoa class cluster如何使用一个小技巧,一个涵盖的有一个的关系。

Or you can subclass NSDictionary — not very trivial, as NSDictionary is a class cluster. But luckily Matt shows in his fantastic blogpost "OrderedDictionary: Subclassing a Cocoa class cluster" how to use a little trick, a covered has-a relationship.

请注意,您的代码

 NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {

    if ( first < second ) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( first > second ) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }

  }];

不会这样做,你想要它做什么,因为你正在将C运算符应用于对象。现在他们的指针将被订购。

wont do, what you want it to do, as you are applying C-operators to objects. Now their pointers will be ordered.

它应该类似于

 NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {
    return [first compare:second];
  }];

或者如果你想在标量上订购,那就是包装作为对象(即NSNumber)

or if you want to order on scalars, that are wrappers as objects (ie NSNumber)

 NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) { 
    if ([first integerValue] > [second integerValue]) 
        return (NSComparisonResult)NSOrderedDescending;

    if ([first integerValue] < [second integerValue])
        return (NSComparisonResult)NSOrderedAscending;
    return (NSComparisonResult)NSOrderedSame;
}];

这篇关于按字典值将NSDictionary键排序到NSArray中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆