简体   繁体   English

iOS基于浮点值排序数组

[英]iOS Sort array based on float value

I wish to sort an array data based on height parameter which is in float. 我希望根据float参数中的height参数对数组数据进行排序。 I used NSSortDescriptor but it isn't working. 我使用了NSSortDescriptor但它无法正常工作。

Following is my code: 以下是我的代码:

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

How do I solve this? 我该如何解决这个问题?

edited: log 编辑:日志

(
    {
    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: returns the sorted array. -sortedArrayUsingDescriptors:sortDescriptors:返回已排序的数组。 So you are basically just sorting it and discarding the result. 所以你基本上只是对它进行排序并丢弃结果。

You need to sort and assign this value to the NSDictionary you are pulling it out of. 您需要对此值进行排序并将其分配给您正在将其拉出的NSDictionary

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

But to do that you need to have a NSMutableDictionary . 但要做到这一点,你需要有一个NSMutableDictionary If you post more code in your question it might help. 如果您在问题中发布更多代码,则可能有所帮助。 But the above is your problem :) 但以上是你的问题:)

EDIT: As per discussion in the comments the values are stored as NSString s 编辑:根据评论中的讨论,值存储为NSString

The following will try to convert any strings as numbers (and any other types): 以下将尝试将任何字符串转换为数字(以及任何其他类型):

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

If you can guarantee that the Height value will always be an NSString or NSNumber (and not a NSNull if we're dealing with JSON) the following sort descriptor would also work: 如果你能保证Height值永远是NSStringNSNumber (如果我们处理JSON,则不是NSNull ),以下排序描述符也可以工作:

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

While the first sort descriptor is a bit longer, its also far more robust in case any other objects are put inside of the Height value as then you would get a class is not key value coding-compliant for the key length error. 虽然第一个排序描述符有点长,但是如果将任何其他对象放在Height值中,它也会更加健壮,因为你会得到一个class is not key value coding-compliant for the key length错误的class is not key value coding-compliant for the key length

Try this it may work. 试试这个可行。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Height"
                                                               ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [[[appDelegate.arr objectAtIndex:section] objectForKey:@"arrPerson"]sortedArrayUsingDescriptor:sortDescriptors];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM