flux
flux

Reputation: 395

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?

Upvotes: 0

Views: 144

Answers (2)

Lucas Eduardo
Lucas Eduardo

Reputation: 11675

The best way to sort an array of objects by some property of the object, its using NSSortDescriptor. In initWithKey, you can set the name of the property that you want to sort.

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".

You can change the ascending value from NO to YES, depending what type of order do you want.

Please, let me know if worked or not.

Upvotes: 3

Rob Napier
Rob Napier

Reputation: 299455

I'm not seeing the bug immediately, but you're reinventing the wheel here. The correct tool for this is 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).

Upvotes: 1

Related Questions