July 30, 20187 yr I have a question about sorting in a stream. When sorting using a single comparator, o is an RS2Object and the reference to realDistance is found. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance)).findFirst(); When I try to use a second comparator to tie-break, o is an object and the reference to realDistance and bar are not found. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance).thenComparingInt(foo::bar).findFirst(); I am also unable to reverse the result of the sort. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance).reversed()).findFirst(); What is the correct way to tie-break a sort in a stream?
July 30, 20187 yr 2 hours ago, Solzhenitsyn said: When I try to use a second comparator to tie-break, o is an object and the reference to realDistance and bar are not found. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance).thenComparingInt(foo::bar).findFirst(); your parentheses are messed up lol. You're welcome
July 30, 20187 yr Author 11 hours ago, dogetrix said: your parentheses are messed up lol. You're welcome I typed an MCVE by hand, parentheses are not the problem. Edited July 30, 20187 yr by Solzhenitsyn
August 4, 20187 yr On 7/30/2018 at 5:03 AM, Solzhenitsyn said: I have a question about sorting in a stream. When sorting using a single comparator, o is an RS2Object and the reference to realDistance is found. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance)).findFirst(); When I try to use a second comparator to tie-break, o is an object and the reference to realDistance and bar are not found. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance).thenComparingInt(foo::bar).findFirst(); I am also unable to reverse the result of the sort. objects.getAll().stream.sorted(Comparator.comparingInt(map::realDistance).reversed()).findFirst(); What is the correct way to tie-break a sort in a stream? Let's see your implementation of bar in foo? Make sure you specify the type as RS2Object for the first functional interface input (should carry to the chained thenComparing) Comparator.<RS2Object>comparingInt(map::realDistance) Also be aware that sorting all objects by realDistance is very computationally expensive.
Create an account or sign in to comment