按数字值排序的NSDictionary键 [英] NSDictionary keys sorted by value numerically

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

问题描述

我将名称作为键和分数作为值存储到 NSDictionary 中,以保存在 NSUserDefaults 中。然后我想回到按分数排序的键,但我似乎不能按数字排序,只有字符串。例如,分数列表100,50,300,200,500给我100,200,300,50,500。



这可以做

  NSString * defaultNames [] = {@Matt,@Terry Jessica,@Sean,nil}; 
NSNumber * defaultScores [] = {@600,@500,@100,@50,nil};

NSDictionary * newScoreDict = [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames count:7];

NSArray * currScores = [scoreDict keysSortedByValueUsingSelector:@selector(compare :)];如何使用keysSortedByValueUsingSelector(NSDictionary)

如何使用keysSortedByValueUsingSelector(NSDictionary)


解决方案

看起来像XCode中的文档所需


I store names as keys and scores as values into an NSDictionary for saving in NSUserDefaults. I then want to get back the keys sorted by score, but I can't seem to sort them numerically, only by string. The list of scores 100, 50, 300, 200, 500, for example, gives me 100, 200, 300, 50, 500.

Can this be done or do I need to go about this differently?

NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil};
NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil};

NSDictionary *newScoreDict =  [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames count:7];

NSArray *currScores = [scoreDict keysSortedByValueUsingSelector:@selector(compare:)];

解决方案

how about using keysSortedByValueUsingSelector (NSDictionary)

Seems to be what you need as per the documentation in XCode

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

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