如果我尝试通过 playerScore 属性对 BL_Player 进行排序:
NSArray *sortedPlayers = [players sortedArrayUsingComparator: ^(BL_Player *a1, BL_Player *a2) {
return [a1.playerScore compare:a2.playerScore options:NSNumericSearch];
或
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];
}];
两者都返回错误的接收器类型。对整数或 NSInteger 进行比较有什么问题?
Best Answer-推荐答案 strong>
试试这个:
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;
}
根据您的要求进行更改。
可以帮到你。
关于ios - 对 NSArray 中的自定义对象进行数字排序时遇到问题,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/30254381/
|