This is a great tip, but it comes with a few caveats. If your numbers can be extremely large or small, you can run into integer overflow which can result in a wrong answer. Make sure you ask your interview about the domain of the numbers you'll be comparing before considering this method. This is only a problem if your number data-types allow overflow.
@tythedev95824 жыл бұрын
Can't use the b.score - a.score trick if negative values are included although.
@dahui586 жыл бұрын
So the y value maps to parameter b? Seems to me the x and y are opposite to the ordering of the params of the method
@RayRay-oi1cc3 жыл бұрын
Be careful with subtractions if values can be negative. An if else check to see which is lesser, greater or equal will reduce the likelihood of bugs .
@johnaweiss Жыл бұрын
2:06 I understand why the sign alone will tell you which one is bigger, but doesn't the caller need to interpret the sign? Would be helpful if the video showed the use of `compare`. Equally simple, i think, would be `return a.score > b.score` which, instead of positive or negative return, would give a true or false return. I wonder if that might use fewer CPU cycles or less RAM, since you're not doing any calculation.
@karimzuniverse5 жыл бұрын
Shouldn't this be a.score - b.score?
@sauravsamant10235 жыл бұрын
i guess she is sorting in descending order, even in which case it should be -1*a.name.compareTo(b.name).
@BrajeshKumar-np1br5 жыл бұрын
@@sauravsamant1023 @0.41 the scores appear in descending order but the names do appear in the ascending order so she didn't multiply a.name.compareTo(b.name) by -1.
@akashpalanisamy56594 жыл бұрын
Thanks for this... Was stuck here during Practise and this really helped me
@Najibway6 жыл бұрын
what language is this in? C#?
@nandhannatarajan21276 жыл бұрын
Java
@johnaweiss Жыл бұрын
What sort of syntax is `Player a`?
@vamsikrishna-vz7yy8 ай бұрын
Player is a user defined data type
@avideosomeday39133 жыл бұрын
I think this is comparing not sorting? Thanks anyway
@Andrezito9172 жыл бұрын
Is there to do this in python?
@toniok.4726 Жыл бұрын
you can do these thing in any lang just a little bit diff
@jaapdebouw74103 жыл бұрын
dankeschon
@deli57773 жыл бұрын
my teacher is making me do this with a non alphabetical order 😵 the internet hasn't been helpful
@CreativeFunction7 жыл бұрын
Comparator.comparing(Player::getScore).reversed().thenComparing(Player::getName) is how I would write this in java 8. You might need to split that out into a variable for the comparator due to .reversed() returning a comparator of T and not player.
@jackbenimble999997 жыл бұрын
Java 8 is like whole new language to me. Can you break this down?
@ko-Daegu6 жыл бұрын
Jack B. Nimble Not exactly first you need to study lambda expressions after check this one out this is not lambda expression though but it’s helpful first to know that ... Also check streams ... Also check new stuff in collection interface .... This is like big stuff in java 8 Also check try with resources You can look it up in youtube like this Try-resources