Algorithms and Data Structures · Junior · Question 35 of 75

What should the comparator (a, b) => ... return for sort to put a before b?

Explanation

A negative value means a goes first, a positive value means b goes first, and zero keeps their relative order. That's why (a, b) => a - b sorts numbers in ascending order.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 35

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz