简体   繁体   English

无法对自定义对象数组进行排序

[英]Trouble sorting an array of custom objects

So I have an array of custom "Element" objects (hey hold atomic number, chemical symbol, atomic mass, etc...) and I am having trouble sorting them by one of their properties; 因此,我有一个自定义“元素”对象的数组(嘿,拥有原子序数,化学符号,原子质量等),并且无法按其属性之一对它们进行排序;

Here is the code: 这是代码:

switch (sortDescriptor) {

        case 0: {
            //Sort the array by "ATOMIC NUMBER"
            NSArray *sortedArray = [self.elementsArray sortedArrayUsingComparator:^(id a, id b) {
                NSNumber *first = @([(SAMElement *)a atomicNumber]);
                NSNumber *second = @([(SAMElement *)b atomicNumber]);
                return [first compare:second];
            }];
            self.elementsArray = [sortedArray mutableCopy];
        }

        case 1: {
            //Sort the array by "ELEMENT NAME"
            NSArray *sortedArray = [self.elementsArray sortedArrayUsingComparator:^(id a, id b) {
                NSString *first = [(SAMElement *)a elementName];
                NSString *second = [(SAMElement *)b elementName];
                return [first compare:second];
            }];
            self.elementsArray = [sortedArray mutableCopy];
        }
        case 2:{
            NSLog(@"sorting by chemical symbol");
            //Sort the array by "CHEMICAL SYMBOL"
            NSArray *sortedArray = [self.elementsArray sortedArrayUsingComparator:^(id a, id b) {
                NSString *first = [(SAMElement *)a  chemichalSymbol];
                NSString *second = [(SAMElement *)b chemichalSymbol];
                return [first compare:second];
            }];
            self.elementsArray = [sortedArray mutableCopy];
        }

        case 3: {
            //Sort the array by "ATOMIC MASS"
            NSArray *sortedArray = [self.elementsArray sortedArrayUsingComparator:^(id a, id b) {
                NSNumber *first = [(SAMElement *)a atomicMass];
                NSNumber *second = [(SAMElement *)b atomicMass];
                return [first compare:second];
            }];
            self.elementsArray = [sortedArray mutableCopy];
        }
        default:
            break;
    }

When is sorts it returns a totally random list of elements. 当进行排序时,它返回一个完全随机的元素列表。 Am i doing something wrong? 难道我做错了什么?

The best way to sort an array of objects by some property of the object, its using NSSortDescriptor. 按对象的某些属性对对象数组进行排序的最佳方法是使用NSSortDescriptor。 In initWithKey , you can set the name of the property that you want to sort. initWithKey ,您可以设置要排序的属性的名称。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"atomicNumber" ascending:NO];   
[self.elementsArray sortUsingDescriptors:@[sortDescriptor]];

In your case, just copy this code above in each case section of your switch statement, changing the key for @"elementName" and @"chemichalSymbol" . 就您而言,只需将以上代码复制到switch语句的每个case部分中,即可更改@"elementName"@"chemichalSymbol"

You can change the ascending value from NO to YES , depending what type of order do you want. 您可以将ascending值从NO更改为YES ,这取决于您想要哪种类型的订单。

Please, let me know if worked or not. 请让我知道是否有效。

I'm not seeing the bug immediately, but you're reinventing the wheel here. 我没有立即看到该错误,但您是在这里重新发明轮子。 The correct tool for this is sortedArrayUsingDescriptors: 正确的工具是sortedArrayUsingDescriptors:

[self.elementsArray sortedArrayUsingDescriptors:@[ 
                 [NSSortDescriptor alloc] initWithKey:@"atomicNumber"] ascending:YES]
                                                  ]];

Try that and see if it gets rid of your bug. 尝试一下,看看它是否摆脱了您的错误。 If you're getting random orders, that usually suggests that your comparitor is inconsistent (sometimes A>B and sometimes B>A for the same A&B). 如果您收到随机订单,通常表明您的比较器不一致(对于相同的A&B,有时A> B,有时B> A)。

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

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