Algorithms: Sort An Array with Comparator

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi, I'm Gayle Laakmann McDowell, author of Cracking the Coding Interview. Today I'm going to build a comparator that we can use to sort an array. So a lot of interview questions are almost annoyingly abstract. This one is actually super applicable. There's a lot of reasons why you might want to implement your own comparator and one of the most common reasons is that you're sorting some, an array of some type, and the computer doesn't know how to sort it unless you tell it. So it doesn't know how to pick which element should appear first. And that's what this case is. So we have this player class and we want to sort it such that the higher scoring person appears first and if they don't, if they have the same score, then fall back on the names. So let's refresh ourselves on what this return value of compare means. So here's what it means. We should return a negative value if we want X to appear first, 0 if they can appear in either order, and a positive value if X is considerably bigger and than Y. So here our logic is, we want to say if they have the same score then use the names. Otherwise we want to say if a dot score is bigger than b dot score then we actually want to return negative 1, then a should appear first. Otherwise, return 1. So we can do this and this works here but there's actually a simpler or shorter way of doing it anyway. We can do return b dot score minus a dot score, so note here that, I'm gonna write this, if x should appear first x should appear a second. So note here that the sign is what matters, the actual value doesn't matter. So doing this will give a consistent sign with this one because note that a dot scorer is bigger than b dot score, this value will in fact be negative. If a dot score is smaller than b dot score than this value will be positive. So it gives a consistent sign with this longer bit of code. So that's all we have to do. Now let's talk about falling back on the names here. Well a really simple way of doing this is, you know, actually just fall back on the names. So return whatever, return, just use the string comparison. So return string dot compared to, or a dot name dot compare to b dot name. And that's as simple as it is. So if you look at how different compare operations are, methods are written, you'll see this kind of logic a lot where we do subtraction. So it's a useful kind of tip to know it can make your code a little bit shorter and cleaner so this is a really useful thing to know how to do, if not for your in your interviews then at least for real life. There's, the syntax will of course vary from language to language, but most languages should, at least most modern languages should have some sort of concept like this, where you can give a, give the built-in sorting algorithm some way of sorting a type that otherwise wouldn't know how to type. So look this up in whatever language you're going to be using. Super useful for a lot of different situations. Good luck.
Info
Channel: HackerRank
Views: 81,509
Rating: undefined out of 5
Keywords:
Id: SzzSwvQfKyk
Channel Id: undefined
Length: 3min 47sec (227 seconds)
Published: Tue Sep 27 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.