Sorting NSMutableArray of NSNumber objects

Discussion in 'iOS Programming' started by aztec31, Sep 16, 2008.

  1. macrumors newbie

    Sep 15, 2008
    How would I sort a small NSMutableArray that has NSNumber objects. I want to arrange the array in ascending order so the smaller numbers are at the beginning of the array. I looked at the docs but there isn't much examples that are clear. Can someone provide an example?
  2. thread starter macrumors newbie

    Sep 15, 2008
    I kind of figured it out. I used
    [myArray sortUsingSelector: @selector(compare:)];

    but could someone explains what @selector means or does?

    By the way this is not for a homework assignment.
  3. Moderator emeritus


    Jul 24, 2002
    @selector gets a reference to the function/method that will be called to implement that selector name.
  4. macrumors member

    Jun 27, 2008
    Assuming myArray is of type NSMutableArray

    NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"property_or_method_of_objects" ascending:YES];
    [myArray sortUsingDescriptors:[NSArray arrayWithObject:mySorter];
    So take a look at the API documents for NSSortDescriptor for adjustments you may need for your specific case.

Share This Page