NS字典排序问题

发布于 2024-11-26 06:23:46 字数 767 浏览 1 评论 0原文

我有一个 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;
 }

提前致谢。

I have an array of 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);

I am getting an output like this.

dictionary objects {

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

but I want to have output like this.

dictionary objects {

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2024-12-03 06:23:46

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

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

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

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

keysSortedByValueUsingSelector returns a sorted array containing dictionary's keys, you have to use this returned array to retrieve the associated objects:

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

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

for (NSString *key in sortedKeys) {
  NSLog(@"%@: %@", key, [dictionary objectForKey:key]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文