This lesson, we are going to be sorting that deck of cards we built in the last lesson.
So creating a function in order to do the sort.
And just like with many things in JavaScript.
There are a number of ways to randomize array content.
And that's going to call this one shuffle.
It's going to pass in cards.
And I know this is a global value, so we don't necessarily need to randomize that.
But we can if we want end.
If you want to update that, we can do that as well, where we can shuffle that that array in place.
So this will make it more flexible, like if you want to use the shuffle somewhere else.
You can just simply copy the function.
And what we're going to be using is the array sort and arrays sort.
So those of you that are familiar, the sorts, the element of an array in place.
So that means that it takes that every object.
And if you do months sort, it's going to output that in a different order.
And every time you sort it, it's going to output it within that sorted order.
So you see here, it's alphabetical.
And here it's putting the numbers.
So smallest.
And also note that it goes one hundred thousand twenty, one thirty and four because it sorts it according
to first character.
So even the digits, it takes them as string values.
And it does the sort that way.
So there is another option to kind of extend upon what sort has to offer.
And that's the compare function.
So the compare function.
So you can pass in some values into the sort and you can run a compare function function.
And this is optional.
And it specifies a function that defines the sort order.
If it's submitted that rate elements are converted to strings, then sorted according to each character's
Unicode code point value.
So that's what's happening over here, where by default, it's sorting it by that Unicode code point
value.
And they are being converted to string.
So that's why you can't sort the numbers.
So if you do want to do that kind of sort or if you want to randomize the content of the sort.
You can do pacien those first element values for comparison and then the second element values for comparison.
They've got some examples here.
And this one is fairly complex to understand.
But if you do look at what the examples they have here, A is less than B by some ordering criterion
and then A is greater than B by the ordering criterion.
So to see they're returning a negative one or a one.
And if it's not if they're equal, it's returning a zero.
So when you do a compare numbers A and B, it's going to do a comparison of those numbers.
So if instead of returning back negative one, one or zero, we randomize this using math random and
just do a dot, a point five.
And then we multiply it by where we subtract the math random.
It will leave it either be a plus or a minus.
And that will randomize our sort order.
So let me show you how this is going to work.
And there's more information, of course, available.
The Mozilla Developer Network sets the document that we're just looking at.
So we're taking in the value of whatever the cards object is.
So that's being passed into this function.
And then we've got the sort of an anonymous function within the sort.
And within this function, we're going to return back.
Point five minus math, random.
And then we can also console log that out.
So you can see the different values that are being returned back.
And it's actually not going to be exactly that.
But this is just so you have an idea of what we see within the console.
And then this function is just going to return back that card's object.
So it actually doesn't have to return it because the sort is in place.
But we're passing in that card object.
And then we're sending that whenever we've constructed the cards actually allowed in one more function
there so that we can see that randomize deck.
So now when we open up the deck of cards, they've been randomized.
So it's no longer that same order.
And every time I refresh it, I'm going to have a different randomized order of the deck of cards.
You can see all of the values that were being returned back.
So in most of the cases.
So we're getting a point.
We've got negatives.
We've got positives.
And that's what we're really looking back at, how the content is being sorted and see all of the numbers
being returned back in the console.
So, of course, it's not the exact number.
And within the return, it's just important to note that either it's doing a negative or a positive,
and that's going to be affecting the sort order.
And it's randomise just using the math random method that's available.
JavaScript got rid of this one, and that's actually it for randomizing the deck of cards.
So this was a really quick function and a quick way to do randomization of the contents of an array.