Sort Numbers With Comparators

In the last lesson, you chose between a chain, forEach(), and a plain loop for each job on the watchlist. Through all of it, the films stayed in the order the friends added them. Tomás now wants to put the running times in order, longest first, so he can see at a glance which films might be too long for a school night.

In this lesson, you’ll sort an array of numbers by handing sort() a function that compares two items. You’ll be able to predict what a sort call does, with and without that function, and whether the original array changes. Start with what most people try first, which is calling sort() with nothing at all.

Sort the Runtimes With sort()

Arrays have a sort() method that puts their items in order, so it looks like the right tool for Tomás’s runtimes. Create a file named runtimes.js. To keep the script short, store the six running times from the watchlist as plain numbers, in the order the friends added the films. You could build the same array with watchlist.map((film) => film.minutes):

Language: JavaScript Filename: runtimes.js
const runtimes = [104, 125, 99, 116, 105, 148];

runtimes.sort();
console.log(runtimes);

Before you run it, write down the array you expect the last line to log. Then run the script with node runtimes.js and open the box below to compare:

Language: Shell
$ node runtimes.js
[ 104, 105, 116, 125, 148, 99 ]

The five three-digit runtimes are in order, smallest first. The 99-minute film, The Grand Budapest Hotel, is the shortest on the list, but it comes last. If you predicted [ 99, 104, 105, 116, 125, 148 ], you expected sort() to compare numbers. It didn’t.

Why would a shorter film end up after a longer one? You can see what sort() compares with a few lines in the Node.js REPL. Compare the two values as numbers, then as strings:

Language: JavaScript
> 99 < 148
true
> "99" < "148"
false
> [10, 9, 1].sort()
[ 1, 10, 9 ]

Comparing strings, JavaScript looks at the first character of each one. The character "1" comes before "9", so "148" comes before "99", whatever the rest of the string says. For lowercase words, it’s the same rule that puts “apricot” before “fig” in a dictionary, even though “apricot” is the longer word.

That’s what sort() does when you give it nothing else. Behind the scenes, it turns each item into a string and compares the strings character by character. A list of lowercase words like these sorts the way you’d expect.

The dictionary comparison stops working at capital letters. The unaccented capitals A to Z sort before every lowercase letter, so “Fig” would land before “apricot”.

A list of numbers sorts by its digits, which is why 10 lands before 9. The method doesn’t check what kind of items the array holds, so it never warns you.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.