NSdictionary排序问题

时间:2011-07-25 08:05:10

标签: iphone nsdictionary

我有一个NSDictionary数组。

NSDictionary*  dictionary = [NSDictionary dictionaryWithObjects:bothUserName forKeys:bothUID];  // here array "bothUserName" and "bothUID" is an NSArray type


[dictionary keysSortedByValueUsingSelector:@selector(compare:)];

NSLog(@" dictionary objects %@",dictionary);

我得到这样的输出。

dictionary objects {

 14172368 = webtickle;
271882407 = electrodealio;
314125883 = Coral5mz;
316212228 = ajaysinghHF2;
316348693 = Caroline99a;
43944597 = WorldStuffer;
 }

但我希望得到这样的输出。

字典对象{

 316212228 = ajaysinghHF2;
316348693 = Caroline99a;
314125883 = Coral5mz;
271882407 = electrodealio;
14172368 = webtickle;
43944597 = WorldStuffer;
 }

提前致谢。

1 个答案:

答案 0 :(得分:3)

keysSortedByValueUsingSelector 返回包含字典键的有序数组,您必须使用此返回的数组来检索关联的对象:

NSDictionary*  dictionary = [NSDictionary dictionaryWithObjects:bothUserName forKeys:bothUID];  

NSArray *sortedKeys = [dictionary keysSortedByValueUsingSelector:@selector(compare:)];

for (NSString *key in sortedKeys) {
  NSLog(@"%@: %@", key, [dictionary objectForKey:key]);
}