Jump to content

Sorting tie-breakers in stream


Solzhenitsyn

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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