Resolved Array Sorting Question

Discussion in 'iOS Programming' started by larswik, Sep 3, 2012.

  1. larswik, Sep 3, 2012
    Last edited: Sep 3, 2012

    larswik macrumors 68000

    Sep 8, 2006
    I have an NSMutableArray with NSDictionary's in them. Each NSDictionary has an Key/Value of combatDate = 1234 for example. This value is the seconds since the game was started. The value in the dict is an NSNumber.
    I am trying to sort the NSMutableArray by the different values in the NSDictionary 'combatDate' from lowest to highest value.

    I am using this sort code which is not working.
        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"combatDate"  ascending:YES];
        [tempArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
        newArray = [tempArray copy];
    But when I do an NSLog to check the order of the new array it is not correct, here is the full list

    What am I missing?
  2. MattInOz, Sep 3, 2012
    Last edited: Sep 3, 2012

    MattInOz macrumors 68030


    Jan 19, 2006
    - (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors
    Your not capturing the results of the sort into an NSArray object.
    So newArray is a copy of the unsorted array.
  3. larswik thread starter macrumors 68000

    Sep 8, 2006
    Problem solved, thank you. First time playing with NSSortDescriptor.

Share This Page