NSSortDescriptor -- 按 intValue 排序,末尾为 0

NSSortDescriptor -- sort by intValue with 0 at the end

我想根据字段的 intValue 使用 NSSortDescriptor 对数组进行排序。使用以下代码可以完美运行:

        descriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"lowerPrice.floatValue" ascending:YES];

唯一的问题是这个字段无论如何都包含一个值,所以有些值是 0。但是,在我的排名中,0 绝对应该排在最后。基本上,0.1 应该是第一个,一直到 50,然后是 0。但是按升序使用 floatValue,我显然首先得到 0。

解决这个问题最优雅、最简单的方法是什么?

使用 sortedArrayUsingComparator: 获取一个块,调用该块来比较元素对和 return 它们的顺序。在块中,首先测试零和 return 适当的排序(两个零相等,一个零大于其他任何东西),如果没有零 return 基于真正的数字排序。

HTH