gpt4 book ai didi

java - 为什么在此逻辑中使用 -1,1,0 ?

转载 作者:行者123 更新时间:2023-12-02 10:29:21 24 4
gpt4 key购买 nike

Collections.sort(al,(s1,s2)->{

return s1.id<s2.id?-1:s1.id>s2.id?1:s1.marks>s2.marks?-1:0;
});

为什么在这个逻辑中使用-1,1,0,请告诉我

最佳答案

我重写了这个

return s1.id<s2.id?-1:s1.id>s2.id?1:s1.marks>s2.marks?-1:0;

下面这段代码更容易理解

if (s1.id < s2.id)
{
return -1;
}
else
{
if (s1.id > s2.id)
{
return 1;
}
else
{
if (s1.marks>s2.marks)
{
return -1;
}
else
{
return 0;
}
}
}

关于-1、0、1,请阅读Comparator

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

关于java - 为什么在此逻辑中使用 -1,1,0 ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com