JavaScript, inside out · Chapter 12 of 51

Sorting and searching

By the end of this chapter you can put a list in any order you need, and find an item in a sorted list of a million in about twenty looks. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The menu
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Anjali's new screen shows the menu by price, cheapest first, and Ravi spots the problem: the ₹100 flask sits above the ₹25 cutting chai. Ravi also wants the kitchen screen to show rush orders first and then the oldest, and Meera wants the loyalty board ranked by visits, with the customer who signed up first winning a tie. And the supplier's sorted price list has a million rows: Ravi wants to look a price up in it without reading every row.

With what you know so far, you would order each list by hand: find the smallest, splice it out, push it into a new list, and repeat. That is a page of code per screen, and it wrecks the list everyone else is reading.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: sortPrices, prices sorted as numbers

    Write sortPrices(prices). Return a new array with the same prices from cheapest to dearest, compared as numbers, not as text: sortPrices([40, 25, 100, 5]) gives [5, 25, 40, 100], and sortPrices([10, 9, 1]) gives [1, 9, 10].

    An empty list gives []. Do not change the array you are given: the check passes a frozen one, and sorting a frozen array in place throws an error.

  2. Task 2

    sortByName: the menu A to Z, ignoring capitals

    Write sortByName(items). Each item is an object with a name. Return a new array of the same item objects sorted by name from A to Z, the way a dictionary orders words, so capital letters do not matter: 'bun maska' comes before 'Cutting chai'.

    Do not change the array you are given: the check passes a frozen one.

  3. Task 3

    kitchenOrder: rush orders first, then the oldest

    Write kitchenOrder(orders). Each order has an id, rush (true or false) and minute (the minute it was placed). Return a new array of the same orders for the kitchen screen: every rush order before every order that is not a rush, and inside each of those two groups the oldest first, which means the smallest minute first.

    Do not change the array you are given: the check passes a frozen one.

  4. Task 4

    leaderboard: most visits first, ties in sign-up order

    Write leaderboard(customers). Each customer has a name and visits. Return a new array of the same customers, most visits first. Customers with the same number of visits must stay in the order they have in the list you are given (their sign-up order), so the comparator must return 0 for a tie.

    Do not change the array you are given: the check passes a frozen one.

  5. Task 5

    binarySearch: find a price by halving the list

    Write binarySearch(sorted, target). sorted is a list of numbers from small to large. Return the position of target in it, or -1 when it is not there: binarySearch([5, 10, 25, 40, 100], 40) gives 3, and binarySearch([5, 10, 25, 40, 100], 30) gives -1. An empty list gives -1, and a list of one item works too.

    Use binary search. The check runs it on a sorted list of a million numbers and counts every item your function reads: more than 60 reads in one search fails, so indexOf, includes, find or visiting every item will not pass.

What you will learn

  • sort with a comparator: put numbers, names and objects in the order you choose.
  • toSorted: the same, on a new array, leaving the original alone.
  • Ties and stability: a second key, and keeping the original order when two items are equal.
  • Binary search: find an item in a sorted list by halving it.

More in The menu

Build "Sorting and searching" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.