Skip to main content

JavaScript Array sort — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Array sort lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Sort numbers ascending

    easy10 min

    Sort a list of numbers smallest first and join with commas.

  2. 2

    Sort numbers descending

    easy10 min

    Sort a list of numbers largest first and join with commas.

  3. 3

    Sort names

    easy10 min

    Sort a list of names alphabetically and join with commas.

  4. 4

    Sort objects by a property

    easy10 min

    Sort users by age and show the ages joined by commas.

  5. 5

    Sort without changing the original

    medium10 min

    Sort a copy and show the original order joined by commas.

Find the bug(5)

  1. 1

    Fix the missing compare function

    easy8 min

    The default sort compares values as text, so 10 lands in the wrong place.

  2. 2

    Fix the backwards compare

    easy8 min

    The list comes out largest first when it should be smallest first.

  3. 3

    Fix the original being sorted

    easy8 min

    sort changes the array, so the original order was lost.

  4. 4

    Fix the object compare

    easy8 min

    Objects cannot be subtracted; the property must be compared.

  5. 5

    Fix reverse used to sort

    easy8 min

    reverse only flips the order; it does not sort.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace