Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 984 of the free egghead.io lessons, plus get TypeScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Sorting arrays in TypeScript

3:54 TypeScript lesson by

In this lesson we cover all the details of how to sort a list of items using TypeScript. We also present a few tricks to make your sort logic more readable and maintainable using TypeScript.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson we cover all the details of how to sort a list of items using TypeScript. We also present a few tricks to make your sort logic more readable and maintainable using TypeScript.

Each array in JavaScript has a sort method, specifically designed to sort the array. The sort method will, by default, sort in ascending order using the string Unicode code points. This was fine for sorting string arrays. For example, if I have an array with foo and bar, if I sort the array, bar will come before foo.

There are two big words of caution with the built-in sort method. One is that it mutates an array and returns it. This catches new developers off guard, who think that sort doesn't mutate the original array and only returns a copy. You will certainly find code like this in many beginner-authored JavaScript code bases.

Fortunately, with TypeScript, you can fix such code bases easily by annotating the array as a read-only array of strings. Now, any mutating methods like the sort method will no longer be allowed on this array. You can now fix it easily by creating a copy of the array using the array prototype slice and then calling sort. Now, the original array remains intact when we sort the copied array.

The second word of caution with the built-in sort method is that, by default, it uses string Unicode code points to sort the items. For example, if we have an array of numbers and we go ahead and sort them, you can see that we get unexpected results. For example, 22 is appearing before 3.

Essentially, as far as the comparison behavior is concerned, it is essentially the same as comparing AA with B. In the dictionary, AA appears before B, and similarly, 22 appears before 3.

Whenever you are sorting anything other than strings, you should always pass in a comparer function to the sort method. A comparer function takes any two items within the array and asks you to return a comparison value between the two.

If the first value is less than the second value, then you should return a negative number. If the two values are equal, then you should return 0. If the first value is greater than the second value, then you should return a positive number.

For numbers, all of these requirements can be met by simply returning A - B. You can see that it works as expected. It is conventional to write the comparer function in its short form.

Now that we know how to sort numbers and strings, let's cover how to sort more complex objects. For example, here we have a list of the top movies along with their names and the year in which they were released. I'll go ahead and paste a few of the movies as well.

We can sort these movies in ascending order of the year in which they were released by simply comparing the two year numbers, as we already know. Now, if I go ahead and log out the movies' names, you can see that they are sorted in ascending order of their year of release.

To sort the items in descending order, we simply swap the order between the first and the second values in the comparer function. Swapping these in the parameter clues in the code reviewer to your descending order intent.

One final thing worth mentioning is that the implementation of the sort algorithm is left open to implementing run-time. All processes implemented using algorithms that have an O of N log N average asymptotic run-time.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?