Sort Records by Two Keys

In the last lesson, you sorted the watchlist’s running times with a comparator and kept the friends’ original order with toSorted(). Those runtimes were plain numbers, though. The watchlist itself is an array of film records, and the friends want whole films in order. Tomás wants the most popular first for his shortlist, and Kenji wants them from A to Z so he can find a title quickly.

In this lesson, you’ll write comparators that compare two records by one of their properties, put text in order with localeCompare(), and add a second key that settles ties. You’ll start with votes, the property the friends care about most.

Sort Films by a Field

Subtracting one film record from another doesn’t give a useful number—JavaScript gives you NaN—so the comparator has to pick the number to compare. The sort() and toSorted() methods still hand your comparator two items, a and b, and read the sign of what it returns. With records, a and b are two whole films. Your comparator reaches into each one for the same property and compares those values.

Create a file named order.js and copy the watchlist array from chain.js to the top of the file. Below it, sort the films into a new array with the most votes first. The result holds the same film records. The map() call near the end turns each film into a short label, so the output fits on a few lines:

Language: JavaScript Filename: order.js
const watchlist = [
  { title: "Paddington 2", minutes: 104, genre: "comedy",
    seenBy: ["Tomás", "Aisha"], votes: 3 },
  { title: "Spirited Away", minutes: 125, genre: "animation",
    seenBy: ["Priya"], votes: 2 },
  { title: "The Grand Budapest Hotel", minutes: 99,
    genre: "comedy", seenBy: ["Kenji", "Lucía"], votes: 0 },
  { title: "Arrival", minutes: 116, genre: "sci-fi",
    seenBy: [], votes: 3 },
  { title: "Coco", minutes: 105, genre: "animation",
    seenBy: ["Aisha", "Priya", "Kenji"], votes: 4 },
  { title: "Inception", minutes: 148, genre: "sci-fi",
    seenBy: ["Tomás"], votes: 1 },
];

const mostVotesFirst = (a, b) => b.votes - a.votes;
const byVotes = watchlist.toSorted(mostVotesFirst);
const labels = byVotes.map(
  (film) => `${film.votes} ${film.title}`
);
console.log(labels);

The comparator mostVotesFirst is the b - a pattern from the last lesson, with .votes on both sides. You use toSorted() because the friends still rely on the watchlist’s original order to see whose suggestion came first.

Before you run it, look at the votes again. Paddington 2 and Arrival both have three. Decide which of the two you expect to come first, then run node order.js and open the box below:

Language: Shell
$ node order.js
[
  '4 Coco',
  '3 Paddington 2',
  '3 Arrival',
  '2 Spirited Away',
  '1 Inception',
  '0 The Grand Budapest Hotel'
]

Paddington 2 comes before Arrival, matching their order in watchlist. For that pair, mostVotesFirst returns 3 - 3, which is 0, so the comparator has no preference.

When a comparator returns 0 for two items, sort() and toSorted() keep them in the order they were already in. A sort that keeps tied items in their original order is called stable, and JavaScript guarantees that its array sorts are stable.

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.