user689751
user689751

Reputation:

NSMutableArray sort custom object using selector

@interface PlayerVO : NSObject {
NSString *name;
int duration;
}

@property (nonatomic, retain) NSString *name;
@property (readwrite) int duration;

@end

custom object to be sorted based on the value of duration (Ascending)

in another class, I created a function,

- (NSComparisonResult)sort:(PlayerVO *)otherObject {
if ([self duration] < [otherObject duration]) {
    return NSOrderedAscending;
} else if([self duration] > [otherObject duration]){
    return NSOrderedDescending;
} else {
    return NSOrderedSame;
}
}

Calling function

[data sortedArrayUsingSelector:@selector(sort:)];

Am I doing something wrong here?

Upvotes: 1

Views: 2702

Answers (1)

Nick Forge
Nick Forge

Reputation: 21464

When you use sortedArrayUsingSelector:, the selector you provide will be sent to the objects in the array. So your -sort: method needs to be declared and defined on the class of the objects that you are sorting, not on another class.

Upvotes: 3

Related Questions