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/