ios - 对 NSArray 中的自定义对象进行数字排序时遇到问题
<p><p>如果我尝试通过 <code>playerScore</code> 属性对 <code>BL_Player</code> 进行排序:</p>
<pre><code>NSArray *sortedPlayers = [players sortedArrayUsingComparator: ^(BL_Player *a1, BL_Player *a2) {
return ;
</code></pre>
<p>或</p>
<pre><code>NSArray *sortedPlayers = [players sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSInteger first = [(BL_Player *)a playerScore];
NSInteger second = [(BL_Player *)b playerScore];
return ;
}];
</code></pre>
<p>两者都返回错误的接收器类型。对整数或 NSInteger 进行比较有什么问题? </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个:</p>
<pre><code>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;
}
</code></pre>
<p>根据您的要求进行更改。</p>
<p>可以帮到你。</p></p>
<p style="font-size: 20px;">关于ios - 对 NSArray 中的自定义对象进行数字排序时遇到问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30254381/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30254381/
</a>
</p>
页:
[1]