I’m implementing some custom NSArray sort selectors and I was wondering whether there’s anything like the <=> operator in C/Objective-C?
I have this:
if (self.count == otherObject.count) return 0;
return (self.count > otherObject.count)? 1 : -1;
and would love to have (as in Perl)
return self.count <=> otherObject.count;
Maybe the
compare:method is what you are looking for?NSString,NSNumberetc implement it. All compare-like methods in Cocoa returns a NSComparisonResult:So you can use the returned integer value directly. Assuming that
countin your question is aNSNumberyou can do:If your case is limited to numbers and you want to use just an operator you can probably use good old minus. But be aware of integer overflow!: