Aru*_*wda 6 java sorting lambda comparable comparator
据我所知,Comparator.comparingInt()应按升序排序,并Comparator.comparingInt().reversed应按降序排序。但是我发现了一种相反的情况。
用一个例子可以更好地解释这一点。以下是我的代码。
金额等级:
class Amount
{
int lineNum;
int startIndex;
Double value;
//Getters , setters and toString.
}
Run Code Online (Sandbox Code Playgroud)
主要方法:
public static void main( String[] args )
{
List<Amount> amounts = new ArrayList<>();
amounts.add( new Amount( 1.0, 5, 10 ) ); //LINE_NUM 5
amounts.add( new Amount( 3.0, 9, 30 ) );
amounts.add( new Amount( 2.0, 3, 40 ) );
amounts.add( new Amount( 9.0, 5, 20 ) ); //LINE_NUM 5
amounts.add( new Amount( 6.0, 1, 50 ) );
amounts.add( new Amount( 4.0, 5, 20 ) ); //LINE_NUM 5
System.out.println( ".............BEFORE SORTING.........." );
amounts.forEach( System.out::println );
amounts.sort(
Comparator.comparingInt( Amount::getLineNum ) //NOTE THIS
. .thenComparingInt( Amount::getStartIndex ).reversed()
.thenComparingDouble( Amount::getValue ) );
System.out.println( "\n\n.............AFTER SORTING.........." );
amounts.forEach( System.out::println );
}
Run Code Online (Sandbox Code Playgroud)
我想要金额列表按lineNum升序,startIndex降序和值升序排序。
所以我的期望是这样。
.............排序后............(期望)
金额[lineNum = 1,startIndex = 50,值= 6.0]
金额[lineNum = 3,startIndex = 40,value = 2.0]
金额[lineNum = 5,startIndex = 20,value = 4.0]
金额[lineNum = 5,startIndex = 20,value = 9.0]
金额[lineNum = 5,startIndex = 10,value = 1.0]
金额[lineNum = 9,startIndex = 30,value = 3.0]
.............排序后............(实际)
金额[lineNum = 9,startIndex = 30,value = 3.0]
金额[lineNum = 5,startIndex = 20,value = 4.0]
金额[lineNum = 5,startIndex = 20,value = 9.0]
金额[lineNum = 5,startIndex = 10,value = 1.0]
金额[lineNum = 3,startIndex = 40,value = 2.0]
金额[lineNum = 1,startIndex = 50,值= 6.0]
除了lineNum order,其他一切都正确。金额按lineNumber降序排序,而我希望它按升序排序。
当我将Comparator更改为以下内容时,结果与预期的一样
amounts.sort(
Comparator.
comparingInt( Amount::getLineNum ).reversed()
.thenComparingInt( Amount::getStartIndex ).reversed()
.thenComparingDouble( Amount::getValue ) );
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为comparingInt( Amount::getLineNum ).reversed()应该按行号降序对金额进行排序。
我注意到的另一件事是,按StartIndex进行比较按预期工作。但是按lineNumber比较不是。
有人可以解释一下吗?
Jon*_*eet 11
如果将每个呼叫放在一行上,则更容易理解发生了什么:
Comparator.comparingInt(Amount::getLineNum)
.thenComparingInt(Amount::getStartIndex)
.reversed()
.thenComparingDouble(Amount::getValue)
Run Code Online (Sandbox Code Playgroud)
这reversed()将返回一个比较器,该比较器将反转其调用的比较器的结果...即“比较器首先比较行号,然后比较起始索引”。这不像是在前一个thenComparingInt()调用的范围内“括起来” ,这就是您以前的格式使它看起来像的样子。
您可以这样做:
Comparator.comparingInt(Amount::getLineNum)
.thenComparing(Comparator.comparingInt(Amount::getStartIndex).reversed())
.thenComparingDouble(Amount::getValue)
Run Code Online (Sandbox Code Playgroud)
到那时,只有开始索引比较被逆转了。
| 归档时间: |
|
| 查看次数: |
326 次 |
| 最近记录: |