Jump to content

Sorting tie-breakers in stream


Recommended Posts

Posted

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?

Posted
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

Posted
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.

  • Boge 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...