Sort can automatically arrange items in an array. In this lesson we look at the basics including how to sort an array of strings alphabetically and the correct way to perform a numerical sort on an array of numbers. We finish as always with a practical use-case that shows not only
sort in action, but also how it can be chained together with other array methods such as
Array Sort sorts the elements in an array in place, and returns the same array. If we wanted to alphabetically sort this array of names, we could simply call items.sort, and then Log Items. You see we get the desired results.
You can do the same with numbers. If we had 10, 30, and 20, that appears to have worked. However, this is not what you expect. When calling sort on an array with numbers, what actually happens is each number is converted to a string, and they're compared using their position in Unicode.
We can demonstrate the problem this causes by placing a 2 here. Now we would expect that 2 is the first item, 10 is the second, and 20 is the third, 30 the fourth. But, when we run this code, we see a very strange result. We have 10 first, then 2, then 20 and 30. This is because, in Unicode, 10 does come before 2. Again, this is because these numbers are being converted to strings first.
To pre-form a numeric sort on an array such as this, we need to provide a comparative function. A comparator has A and B being the left and right item of the sort algorithm. To sort this array in ascending order, we can simply provide A minus B and we get a correct sort.
By providing a comparative function here, you're taking over how two values are compared with each. Whereas previously, these numbers were being converted into strings first, inside our comparative function, we just get the raw values, and that allows us to simply subtract B from A, and return the result of that subtraction to the comparative functions, which is then used to determine what action should be taken on the current item.
The reason why something so simple as this works for a numeric sort, is that this comparative function only cares that you return either 0, or a value less than 0, or a value above 0. For example, the longhand version of this might look something like, If A - B = 0, return 0. If A - B < 0, return -1. If it's more than 0, return 1. [inaudible 02:46] again, you see it still works.
The interesting part here is that this number can be anything that's below zero, and this can be anything that's above zero, and that will still work. This explains why simply minusing B from A works. If we console log that, we can see it in action. You see we get some negative numbers and some positive numbers.
When the underlying algorithm that's performing the sort calls this comparative function, it really only cares if the value is either zero, below zero, or above zero. That's why you can reduce all of this down to a simple expression A - B. To perform a descending sort, you would simply provide B - A, instead.
We can make use of this comparative function in multiple ways. For example, if we go to having an array of names, this time we've added in Kitty, and we wanted to sort this array based on the length of the string. In this case, each value inside the comparator will be a string. Strings have a length property, so we can compare that instead.
You see we have Kitty at the front, as we are using B - A, which gives us a descending order. Now let's look at a slightly more useful example. Let's use an array of objects, where each object represents a lesson as a title and a views count. We want to sort this array based on the amount of views, and then I'll put some HTML.
We'll first create the list by sorting the lessons based on the amount of views. We want it to be in descending order, so that the most popular video is first. We'll call b.views - a.views. This will give us back an array on which we can call map. Then, we'll simply return a template string. We'll indent each line with four spaces, and we'll create an .
Inside of here, we'll say x.title, space, and then in brackets, we'll use x.views. Finally, we can join that together with a newline character, which will make up our list. Then we can create an output variable, which is another template string, which is a , has a new line, then our list, and then another new line.
If we output this to the console, you can see we get the output we expect, which is a list of our items that have been sorted based on the amount of views. To recap, we take the lessons array. We provide a comparative function to the sort method, which sorts each lesson based on how many views it has. Sort gives us back the same array, although now modified.
Then we call map, which is called for each item of the array. We apply a little bit of padding, just for the sake of output here. Then we access the title and the view. We join those together with a newline character, and then we wedge that in between a , with some more newline characters, which produces this output.