我有这样的问题。 我有 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/