2

So I have an array with about 180 dictionaries, There are 5 objects in each dictionary (3 strings and 2 NSNumbers)

I am trying to sort each dictionary by one of its properties, this works fine for the strings but when it comes to the number it does not sort correctly.

 -(void) sortArrayBy:(int)sortingNumber {

    switch (sortingNumber) {
            case 0:
                NSLog(@"sorting by atomicnumber");
                [self.elementsArray sortUsingDescriptors:[NSArray arrayWithObjects:
 // When sorting this comes back the same each time but not in the correct order
                [NSSortDescriptor sortDescriptorWithKey:@"ATOMIC NUMBER" ascending:YES], nil]];
                break;
            case 1:
                NSLog(@"sorting by name");
                [self.elementsArray sortUsingDescriptors:[NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:@"ELEMENT NAME" ascending:YES], nil]];
                break;
            case 2:
                NSLog(@"sorting by symbol");
                [self.elementsArray sortUsingDescriptors:[NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:@"CHEMICAL SYMBOL" ascending:YES], nil]];
                break;
            case 3:
                NSLog(@"sorting by mass");
 // When sorting this comes back the same each time but not in the correct order
                [self.elementsArray sortUsingDescriptors:[NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:@"ATOMIC MASS" ascending:YES], nil]];
                break;

            default:
                break;
        }
6
  • Care to clarify "consistent but not correct", please? '1, 20, 3', for example, is "constitent and correct" for certain applications. Commented Jul 29, 2013 at 9:39
  • Could you give an example of "consistent but incorrect" sorting? Show us a sub-sequence after the sort, and point out the out-of-order items. Commented Jul 29, 2013 at 9:39
  • I mean each time I sort it it comes back the same but the order is incorrect Commented Jul 29, 2013 at 9:40
  • @user2113952 Is there any pattern to that incorrect order, though? Could you show, say, the first ten..fifteen numbers after the sort? Commented Jul 29, 2013 at 9:44
  • I can't see any pattern in the sort Commented Jul 29, 2013 at 9:48

1 Answer 1

4

Ok so I figured out the problem, it was sorting by the first number not the whole number for some reason, so i use this code:

        NSSortDescriptor *sortDescriptor = [NSSortDescriptor 

sortDescriptorWithKey:@"ATOMIC MASS" ascending:YES comparator:^(id obj1, id obj2) {
                return [obj1 compare:obj2 options:NSNumericSearch];
            }];

            [self.elementsArray sortUsingDescriptors:[NSArray arrayWithObjects: sortDescriptor, nil]];
Sign up to request clarification or add additional context in comments.

2 Comments

It was sorting the numbers "alphabetically", meaning that 100 will sort before 2.
That is exactly what my comment inferred. A short example list of 'badly sorted' data would have shown it immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.