Algorithms: Sort An Array with Comparator

  Рет қаралды 83,876

HackerRank

HackerRank

Күн бұрын

Пікірлер: 22
@LilMomo7777
@LilMomo7777 7 жыл бұрын
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.
@tythedev9582
@tythedev9582 4 жыл бұрын
Can't use the b.score - a.score trick if negative values are included although.
@dahui58
@dahui58 6 жыл бұрын
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-oi1cc
@RayRay-oi1cc 3 жыл бұрын
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
@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.
@karimzuniverse
@karimzuniverse 5 жыл бұрын
Shouldn't this be a.score - b.score?
@sauravsamant1023
@sauravsamant1023 5 жыл бұрын
i guess she is sorting in descending order, even in which case it should be -1*a.name.compareTo(b.name).
@BrajeshKumar-np1br
@BrajeshKumar-np1br 5 жыл бұрын
@@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.
@akashpalanisamy5659
@akashpalanisamy5659 4 жыл бұрын
Thanks for this... Was stuck here during Practise and this really helped me
@Najibway
@Najibway 6 жыл бұрын
what language is this in? C#?
@nandhannatarajan2127
@nandhannatarajan2127 6 жыл бұрын
Java
@johnaweiss
@johnaweiss Жыл бұрын
What sort of syntax is `Player a`?
@vamsikrishna-vz7yy
@vamsikrishna-vz7yy 8 ай бұрын
Player is a user defined data type
@avideosomeday3913
@avideosomeday3913 3 жыл бұрын
I think this is comparing not sorting? Thanks anyway
@Andrezito917
@Andrezito917 2 жыл бұрын
Is there to do this in python?
@toniok.4726
@toniok.4726 Жыл бұрын
you can do these thing in any lang just a little bit diff
@jaapdebouw7410
@jaapdebouw7410 3 жыл бұрын
dankeschon
@deli5777
@deli5777 3 жыл бұрын
my teacher is making me do this with a non alphabetical order 😵 the internet hasn't been helpful
@CreativeFunction
@CreativeFunction 7 жыл бұрын
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.
@jackbenimble99999
@jackbenimble99999 7 жыл бұрын
Java 8 is like whole new language to me. Can you break this down?
@ko-Daegu
@ko-Daegu 6 жыл бұрын
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
Algorithms: Bit Manipulation
9:06
HackerRank
Рет қаралды 542 М.
#95 Comparator vs Comparable in Java
15:43
Telusko
Рет қаралды 223 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Data Structures: Heaps
10:32
HackerRank
Рет қаралды 1,3 МЛН
JavaScript Comparator Function | Sorting Explained!
12:21
The Code Creative
Рет қаралды 34 М.
Algorithms: Memoization and Dynamic Programming
11:17
HackerRank
Рет қаралды 976 М.
Data Structures: Linked Lists
7:43
HackerRank
Рет қаралды 1 МЛН
How to fix a broken heart | Guy Winch | TED
12:26
TED
Рет қаралды 10 МЛН
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 574 М.
Java Comparator - What, Why, How Explained!
8:17
Michael Oswald
Рет қаралды 10 М.
Data Structures: Tries
4:55
HackerRank
Рет қаралды 516 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 395 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН