如何按降序对包含数字作为字符串的NSMutableArray进行排序

编程入门 行业动态 更新时间:2024-10-27 07:23:39
本文介绍了如何按降序对包含数字作为字符串的NSMutableArray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在按降序对包含数字作为字符串的 NSMutableArray 进行排序.我的数组格式如下:

I was sorting an NSMutableArray containing numbers as strings, in descending order. My array is in the same form as below:

{273,0,0,0,0,0,48,48,59,254}

我正在寻找按降序排序.预期结果是:

I was looking for sorting it in descending order. That is expected result is:

{273,254,59,48,48,0,0,0,0,0}

我尝试过:

[[[myArray sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];

结果很奇怪.是:

{59,48,48,273,254,0,0,0,0,0} -- zeros are not getting sorted, and not proper sort

然后我尝试:

[myArray sortedArrayUsingSelector: @selector(compare:)];

输出错误(如下所示):

Again wrong output (as below):

{0,0,0,0,0,254,273,48,48,59} -- zeros came first !!! and 273 in wrong position!!!

我也尝试过:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"length" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [myArray sortUsingDescriptors:sortDescriptors];

再次...结果如下:

{0,0,0,0,0,48,48,59,273,254} -- 273 in wrong position

我完全感到困惑.我需要检查那些零吗?那那273是怎么回事?我在哪里错了?有什么适合我的解决方案吗?请帮助我找出问题所在.

I am totally confused. Do I need to check for those zeros? then whats happening with that 273? Am I wrong anywhere? Is there is any proper solution for me? Please help me finding out the issue.

推荐答案

尝试一下.这可能会帮助:

Try this. This may help:

NSMutableArray *myArray = ....... Your mutable array NSArray *sorted_Array = [myArray sortedArrayUsingDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:NO]]];

保持编码............:)

Keep Coding............ :)

更多推荐

如何按降序对包含数字作为字符串的NSMutableArray进行排序

本文发布于:2023-10-22 08:43:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数字   降序   NSMutableArray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!