Skip to content

QuizNot attempted

Spot the bug: sorting only the loaded rows

20 of 500 rows are loaded and the user sorts Cost descending. What do they see?
const sortedData = sortedColumn
? [...displayData].sort((a, b) => {
if (a[sortedColumn] < b[sortedColumn]) return sortDirection === 'asc' ? -1 : 1;
if (a[sortedColumn] > b[sortedColumn]) return sortDirection === 'asc' ? 1 : -1;
return 0;
})
: displayData;
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • The 20 loaded rows reordered; as more pages load, pricier rows appear above the ones already seen
  • Sort first, then paginate: sort the whole dataset and slice it (client side), or send the sort to the server and restart from page 1.
  • The < comparator has its own bugs too: numeric strings compare as text ("10" < "9"), non-ISO dates compare as text, and case matters ("Z" < "a").
  • Not the 20 most expensive: only loaded rows take part in the sort.
  • Not all 500 sorted: recomputing still sorts only displayData.
  • Not an error: spreading state into a new array is the correct way to sort without mutating it.