Sometimes you want to short elements of an array. Unfortunately, there is a predefined function called sword that was designed by JavaScript. And it's going to get the job done for us. So we don't have to create our own algorithm in order to create that sorting function that is going to sort elements, either ascending or descending Li. But the only problem problem with this function is that it works better for strings, and it doesn't really work for integers. In order to make it work for integers, we would have to pass our own function inside it.
But it's, it's still relatively easy. So let's try this function. Now. Here we have an array called names. And it consists of four elements Kate, john, Alice and Bob and the crew. correct order for this array is that Alice, Bob, and then john and then Kate, because alphabetically A of m, B, and then j and being K. So if I use names and then a sort is or C, and I use the console log dot log, and I pass this names, array, what I'm going to get is the correct alphabetical order, which is Alice, Bob.
JOHN Kay. Right. But if I use this function for ages here, if I use this instead of names, ages, and I print ages, look what I'm going to get. What I'm going to get is 1223 5671 80. But nine is at the end. And that doesn't look right does it?
It doesn't look right, because nine should be at the beginning. If we want to sort this ascending, this is why the source function doesn't work for numbers. Therefore, what we need to do is that we need to add our own function, we pass our own function inside the source function in order to make it work for numbers. So here, what I'm going to do inside the source function, what I'm going to do, what I'm going to do is just pass our own function function. And then this function is going to take two parameters. The first parameter is the first number, I'm going to call it first.
And the second parameter is the second number. And what this function are going to do is that it's going to take the first number and as the first and the second number as well as the second and it's going to subtract first Mine is second, and it's going to determine which one is smaller and which one is larger. So what I'm going to do here is just return and then first, first minus second, and semicolon. If now I save and run, I'm going to get 912 2356 71 and 80. And that is an ascending order by just returning the difference between the first minus the second, but if I want a descending order, what I would do is that I'm going to just reverse this, I'm going to, instead of first minus second, I'm going to say second, minus first. And now if I save and run, what I'm going to get is 87 to 136 2312 and nine and that is a descending order.
So the concept behind that is that it's going to take the first number and the second number and it's gonna Subtract 12 minus 23. And if the value is positive, it's going to know that 23 is greater than 12. So it's going to add 12 first and then 23. And it's going to do that for all elements inside this array. And if we want to do the reverse, instead of sorting ascending Li, if we want a descending order, we just would reverse the first minus second by second, minus first