iOS 根据浮点值排序数组

标签 ios sorting

我希望根据高度 参数对数组数据进行排序,该参数是 float 。我使用了 NSSortDescriptor 但它不起作用。

以下是我的代码:

NSSortDescriptor *hDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Height" ascending:YES];

NSArray *sortDescriptors = @[hDescriptor];

[[[appDelegate.arr objectAtIndex:section] objectForKey:@"arrPerson"] sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"%@",[[appDelegate.arr objectAtIndex:section] objectForKey:@"arrPerson"]); // height is printed in random order. not sorted

我该如何解决?

已编辑:日志

(
    {
    Height = "11.5766";
    Name = "abc";
},
    {
    Height = "11.8443";
    Name = "sdfsdf";
},
    {
    Height = "12.211";
    Name = "hnkhjk";
},
    {
    Height = "13.7271";
   Name = "ertert";
},
    {
    Height = "15.2694";
    Name = "sdf";
},
    {
    Height = "21.9242";
    Name = "fgh";
},
    {
    Height = "23.0857";
    Name = "ert";
},
    {
    Height = "6.48365";
    Name = "cvb";
},
    {
    Height = "7.5204";
    Name = "rt";
},
    {
    Height = "8.67856";
    Name = "asd";
}

)

最佳答案

-sortedArrayUsingDescriptors:sortDescriptors: 返回排序后的数组。所以您基本上只是对其进行排序并丢弃结果。

您需要对该值进行排序并将其分配给您要将其拉出的 NSDictionary

NSArray *persons = [[appDelegate.arr objectAtIndex:section] objectForKey:@"arrPerson"];
NSArray *sortedPersons = [persons sortedArrayUsingDescriptors:sortDescriptors];
[[appDelegate.arr objectAtIndex:section] setObject:sortedPersons forKey:@"arrPerson"];

但要做到这一点,您需要有一个 NSMutableDictionary。如果您在问题中发布更多代码可能会有所帮助。但以上是你的问题:)

编辑: 根据评论中的讨论,值存储为 NSStrings

以下将尝试将任何字符串转换为数字(和任何其他类型):

NSArray *sorters = @[[NSSortDescriptor sortDescriptorWithKey:@"Height" ascending:YES comparator:^(id obj1, id obj2) {
        NSNumber *n1;
        NSNumber *n2;
        // Either use obj1/2 as numbers or try to convert them to numbers
        if ([obj1 isKindOfClass:[NSNumber class]]) {
            n1 = obj1;
        } else {
            n1 = @([[NSString stringWithFormat:@"%@", obj1] doubleValue]);
        }
        if ([obj2 isKindOfClass:[NSNumber class]]) {
            n2 = obj2;
        } else {
            n2 = @([[NSString stringWithFormat:@"%@", obj2] doubleValue]);
        }
        return [n1 compare:n2];
    }]];

NSArray *persons = [[appDelegate.arr objectAtIndex:section] objectForKey:@"arrPerson"];
NSArray *sortedPersons = [persons sortedArrayUsingDescriptors:sorters];
[[appDelegate.arr objectAtIndex:section] setObject:sortedPersons forKey:@"arrPerson"];

如果你能保证 Height 值永远是 NSStringNSNumber(而不是 NSNull 如果我们处理的是 JSON),下面的排序描述符也可以工作:

NSArray *sorters = @[[NSSortDescriptor sortDescriptorWithKey:@"Height.doubleValue" ascending:YES]];

虽然第一个排序描述符有点长,但如果将任何其他对象放入 Height 值内,它也会更加健壮,因为那样你会得到一个 class is not key值编码符合 key 长度 错误。

关于iOS 根据浮点值排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23151998/

相关文章:

c# - .NET 中是否有 SortedList<T> 类?

c++ - 使用 lambda 表达式对指向对象的指针列表进行排序

ios - 轻按单元格时更改UICollectionViewCell的大小

ios - 如何按用户或自动获取时间设置 ios

ios - 在键入时获取 UITextField 中的整个文本的简单方法是什么?

javascript - 不同对象的方法无法同时执行(D3.js)

java - 学习元素排序的算法(最好在 Java 中)

java - 哪个占用更多内存 : multiply one variable or increment a second variable?

ios - EXC_BAD_INSTRUCTION Swift 抖动函数

iOS navigationItem titleView 图片和文字