How to sort an NSMutableArray of objects by a member of its class, that is an int or float

Posted by J. Dave on Stack Overflow See other posts from Stack Overflow or by J. Dave
Published on 2010-04-23T17:53:51Z Indexed on 2010/04/23 18:03 UTC
Read the original article Hit count: 189

Filed under:
|
|

I have a class (from NSObject) that contains:

NSString name
int      position
float    speed

I then create an array (NSMutableArray) of objects from this class. I would like to then sort the array by the 'speed' value, which is a float.

I initially has the float value as an NSNumber and the int as NSInteger, and I was successfully sorting with:

[myMutableArray sortUsingFunction:compareSelector context:@selector(position)];

where myMutableArray is my array of objects.

here is the function:

static int compareSelector(id p1, id p2, void *context) {
    SEL methodSelector = (SEL)context;
    id value1 = [p1 performSelector:methodSelector];
    id value2 = [p2 performSelector:methodSelector];
    return [value1 compare:value2];
}

Now that I am using int instead of NSInteger, the above code does not work. Is there a more low-level command that I should be using to execute the sort? Thank!

© Stack Overflow or respective owner

Related posts about nsmutablearray

Related posts about sort