Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Can anyone tell what is the difference between these lines?
Arrays.sort(pairs, (a, b) -> a[0] - b[0]) Arrays.sort(pairs, (a, b) -> a[1] - b[1]);
;
[0]
[1]
Integer::compare
The first sorts based on the first element of the arrays that are being sorted, and the other sorts based on the next element.
If the input was:
[2, 33], [4, 22], [3, 11]
The first would produce
[2, 33], [3, 11], [4, 22]
and the second would produce
[3, 11], [4, 22], [2, 33]
Add a comment
According to your code. It looks that the first line will not execute. Because it is missing the terminator.
Arrays.sort(pairs, (a, b) -> a[0] - b[0]);
And the second line will run and it gives the output according to your input.
Arrays.sort(pairs, (a, b) -> a[1] - b[1]);
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
;at the end. But if we add that;it will try to sort arrays based on values of their first column (accessed via[0]). Second will try to sort based on values of second column (accessed via[1]).Integer::compare