我正在将包含数字的 NSMutableArray 排序为降序。我的数组的形式如下:
{273,0,0,0,0,0,48,48,59,254}
我一直在寻找按降序排序的方法。这是预期的结果是:
{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:)];
输出错误(如下所示):
{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发生了什么?我错了吗?对我来说有适当的解决方案吗?请帮我找出问题。
答案 0 :(得分:2)
试试这个。这可能会有所帮助:
NSMutableArray *myArray = ....... Your mutable array
NSArray *sorted_Array = [myArray sortedArrayUsingDescriptors:
@[[NSSortDescriptor sortDescriptorWithKey:@"intValue"
ascending:NO]]];
保持编码............:)