Solzhenitsyn Posted July 30, 2018 Posted July 30, 2018 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?
dogetrix Posted July 30, 2018 Posted July 30, 2018 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
Solzhenitsyn Posted July 30, 2018 Author Posted July 30, 2018 (edited) 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, 2018 by Solzhenitsyn
FrostBug Posted August 4, 2018 Posted August 4, 2018 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. 1