Introduction to JavaScript

Testing Solution with sort

Introduction to JavaScript

Check out a free preview of the full Introduction to JavaScript course

The "Testing Solution with sort" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian live codes the sort section of the solution, giving various ways to succeed in solving the same problem.

Preview
Close

Transcript from the "Testing Solution with sort" Lesson

[00:00:00]
>> Brian Holt: So, sort. I was initially gonna tell you to do bubble sort, and I think some of you probably end up trying, which is cool, I applaud that. So I'll show you first how to do normal sort, and then we'll go back and do bubble sort. So we listen ascending order.

[00:00:22]
So the easiest way to do this would be, const, new list of some sort or sort of list, let's go sorted lists, sorted list equals, list.sor, and here, where it's gonna take in num1 and num2. You can call it a and b, x and y, red and blue, I don't know, doesn't really matter.

[00:00:48]
Think I showed you this yesterday, if num1 ua greater than num2, return 1, else if num1 less than num2, return negative 1, else. So you can put an else return 0 here, or I mean, this doesn't really matter too much. Otherwise, you can just put return 0 here because, you don't really need an in house clause there, right?

[00:01:25]
Because if it doesn't do this one, and it doesn't do this one, then it's definitely gonna do that one. So sometimes, some people's like lentils, which are code style, checkers will be picky about that. I don't care. It's function does exactly the same thing. So, yeah.
>> Speaker 2: Is there any difference between this, where you wrote here, and writing it as just subtracting num2 from num1?

[00:01:49]
>> Brian Holt: Yeah, I'll show that in just a second. Yeah.
>> Brian Holt: But that's a really cool clever trick, which is why I'm sure they did it that way too.
>> Brian Holt: SortedList. Okay, so that this works as is right here, right. Does this make sense to everyone? Right. If num1 is bigger, then return one.

[00:02:13]
Right. If num2 is bigger, then return negative 1. Otherwise, if they're equal, then you return 0.
>> Brian Holt: So, Emma, here, showed me yesterday, which I thought was really cool. We'll cover that out.
>> Brian Holt: I'll just grab this line right there. Sorted list equals that.
>> Brian Holt: She showed me that you can do return num2 minus num1, and this works just as well.

[00:02:56]
>> Speaker 2: And it does do exactly the same thing.
>> Speaker 2: Maybe not.
>> Brian Holt: Maybe not. Let's see what did we end up with?
>> Brian Holt: Okay, I had to do a num1 minus num2. In any case, why is this? Okay, I guess that does make sense. Well, let's say this is five and this is one, right.

[00:03:36]
So 5 minus 1 is going to be four, which is gonna be a positive number. Which means, that this is going to return just the same that it would return up here. If this is five, and this is one, it's gonna be a positive number, so it returns one here.

[00:03:51]
If they're equal, this is three, and this is three. So 3 minus 3, it's gonna return 0 here, right? So that makes sense. And if this is ten, and this is one, it's gonna say 1 minus 10, that's negative 9, which is a negative number. Works just as well.

[00:04:07]
So a little bit more clever way of doing it.
>> Brian Holt: And again, you can get even more clever about this.
>> Brian Holt: And get that all into one line. A, b, a,
>> Brian Holt: If you wanted to.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now