对 NSMutableArray 进行排序

发布于 2024-09-15 14:41:48 字数 538 浏览 8 评论 0原文

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

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

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

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

,当我按关键字“point”(即字符串)排序时,它给我这样的结果 100> 20> 30> 50 相反应该是 20> 30> 50> 100

有什么想法请说。

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]];

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

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

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

have any idea pleassss.

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

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

发布评论

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

评论(3

葬シ愛 2024-09-22 14:41:48

共有三种解决方案:

  1. 将 NSNumber 实例放入字典中代替您当前拥有的字符串(例如,@"100")。 NSNumber 对象应该在数值上比较它们自己。

  2. 向数组发送 sortUsingComparator:sortUsingFunction:context: 消息而不是 sortUsingDescriptors:,传递检索字符串的块或函数并对它们进行数字比较(通过发送它们 compare:options: 消息与 NSNumericSearch 选项)。

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

  3. 将字典替换为模型对象 属性 代替字典键。如果您使用数字类型声明属性(例如,NSUInteger),那么当数组使用 KVC 访问该属性时,它将获取 NSNumber 对象,正如上面#1 中所述,应该比较 NSNumber 对象自己的数字。

我会选择#3。

There are three solutions:

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

  2. 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.

  3. 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.

I would go with #3.

迷离° 2024-09-22 14:41:48

最好的方法是将您的数字存储为 NSNumbers:

[index addObject:[[NSNumber alloc] initWithInt:number]];

然后您可以使用 [NSNumber Compare:] 方法对数组进行排序:

[index sortUsingSelector:@selector(compare:)];

The best way to do this is to store your numbers as NSNumbers:

[index addObject:[[NSNumber alloc] initWithInt:number]];

Then you can sort your array using the [NSNumber compare:] method:

[index sortUsingSelector:@selector(compare:)];
中二柚 2024-09-22 14:41:48
NSMutableArray* tempArray = [NSMutableArray arrayWithArray:anArray];
[tempArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

我在一些代码中使用这个?这行得通吗?

另外,刚刚注意到您将数组对象设置为字符串,我认为这也是一个问题。尝试将它们添加为 NSIntegers 或 ints...这有效吗?

NSMutableArray* tempArray = [NSMutableArray arrayWithArray:anArray];
[tempArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

I use this in some code? Will this work?

Also, just noticed you are setting the array objects as strings which I'm assuming will also be an issue. Try adding them as NSIntegers or ints... does that work at all?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文