排序NSMutableArray [英] Sort NSMutableArray

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

问题描述

请帮助我
我想对NSMutableArray进行排序,是的,我可以这样做

please help me I want to sort NSMutableArray and yes I can do this by

NSSortDescriptor *sortPoint = [[NSSortDescriptor alloc] initWithKey:@"point" ascending:YES];
[hightScore sortUsingDescriptors:[NSArray arrayWithObject:sortPoint]];

但是在NSMuatableArray(hightScore)中,我有2个NSMutableDictionary这样的

but in NSMuatableArray(hightScore), I have 2 NSMutableDictionary like this

[item setObject:@"player_name" forKey:@"name"];
[item setObject:@"100" forKey:@"point"];

当我按关键字 point对字符串进行排序时,它像这样
100> 20> 30> 50
应该是
20> 30> 50> 100

and when I sort by keyword "point" that is string it give me like this 100 > 20 > 30 > 50 instead should be 20 > 30 > 50 > 100

有任何想法取悦。

推荐答案

有三种解决方案:


  1. 将NSNumber实例放入用字典代替您当前使用的字符串(例如, @ 100 )。 NSNumber对象应该在数值上进行比较。

  1. Put NSNumber instances into the dictionaries in place of the strings (e.g., @"100") you currently have. NSNumber objects should compare themselves numerically.

向数组发送 sortUsingComparator: sortUsingFunction:context:消息而不是 sortUsingDescriptors:消息,传递一个块或函数来检索字符串并对其进行数值比较(按发送 compare:options: 带有 NSNumericSearch 选项的邮件)。

Send the array a sortUsingComparator: or sortUsingFunction:context: message instead of sortUsingDescriptors:, passing a block or function that retrieves the strings and compares them numerically (by sending them compare:options: messages with the NSNumericSearch option).

请注意,块或函数将传递给整个字典,而不是字符串,因为它无法知道要提取的字符串,因此您必须获取自己从字典中取出字符串以进行比较。

Note that the block or function will be passed the whole dictionaries, not the strings, since it has no way to know which strings to extract, so you'll have to get the strings out of the dictionaries yourself in order to compare them.

模型对象具有属性代替字典键。如果用数字类型声明属性(例如, NSUInteger ),则当数组使用KVC访问该属性时,它将获得NSNumber对象,如下所述上面的#1应该在数字上进行比较。

Replace the dictionaries with model objects that have properties in place of the dictionary keys. If you declare the property with a numeric type (e.g., NSUInteger), then when the array uses KVC to access the property, it will get NSNumber objects, which, as noted under #1 above, should compare themselves numerically.

我会选择#3。

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

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