ios - NSMutableDictionary 和 NSArray

标签 ios nsarray nsdictionary

我有这样的问题。 我有 NSMutableDictionary

Object  10          Key  1
Object  20          Key  2
Object  30          Key  3
Object  40          Key  4
Object  50          Key  5

然后我有代码

NSArray*currentArray=[currentMutableDictionary allValues];

当我输出电流数组时 值(value)

20
50
30
10
40

如何以正确的顺序将字典中的对象添加到数组中?

感谢您的回答。

最佳答案

试试这个:

NSInteger intSort(id num1, id num2, void *context)
{
    int v1 = [num1 intValue];
    int v2 = [num2 intValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

 NSArray *currentArray = [[currentMutableDictionary allValues] sortedArrayUsingFunction:intSort context:nil];

假设你的字典中有 NSNumber 对象,这应该正确地排序数组。

关于ios - NSMutableDictionary 和 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5340208/

相关文章:

使用 SOAP 的 IOS Swift 调用 Web 服务

ios - 从 givven 索引开始枚举 NSArray 双向搜索(无回绕)

objective-c - 将数组的每个对象与其他每个对象进行比较

ios - 从字典的 NSMutableArray 获取字典

objective-c - NSDictionary 枚举二级

objective-c - 关于iOS目标版本和SDK版本

iOS - 由于内存问题而终止

ios - 如何检测 AVPlayer 视频何时结束播放?

objective-c - 由 NSMutableArray 支持的 NSArray @property

ios - 给定 JSON 对象的 NSArray,如何在 NSDictionary 中存储同一个键的多个值?