ios - Trouble sorting custom objects in an NSArray numerically -
if i'm trying sort bl_player
playerscore
property:
nsarray *sortedplayers = [players sortedarrayusingcomparator: ^(bl_player *a1, bl_player *a2) { return [a1.playerscore compare:a2.playerscore options:nsnumericsearch];
or
nsarray *sortedplayers = [players sortedarrayusingcomparator:^nscomparisonresult(id a, id b) { nsinteger first = [(bl_player *)a playerscore]; nsinteger second = [(bl_player *)b playerscore]; return [first compare:second options:nsnumericsearch]; }];
both of return bad receiver types. what's wrong doing comparisons on ints or nsinteger?
try one:
nsarray *sortedplayers = [players sortedarrayusingcomparator: ^(bl_player *a1, bl_player *a2) { if (a1.playerscore > a2.playerscore) { return nsorderedascending; } else if (a1.playerscore < a2.playerscore) { return nsordereddescending; } else return nsorderedsame; }
change per requirement.
may you.
Comments
Post a Comment