1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Resolved Array Sorting Question

Discussion in 'iPhone/iPad Programming' started by larswik, Sep 3, 2012.

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

    macrumors 65816

    #1
    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.
    Code:
        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

    macrumors 68030

    MattInOz

    #2
    Code:
    - (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. macrumors 65816

    #3
    Problem solved, thank you. First time playing with NSSortDescriptor.
     

Share This Page