如果我尝试通过 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 进行比较有什么问题?
试试这个:
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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |