FunctionRacerProblemsRace

Sort by Distance From Zero

EasySorting and searchingPython, JavaScript, TypeScript, Java, C++

Return a new list with the items of nums ordered by their absolute value, smallest first. When two items have the same absolute value but different signs, the negative one comes first, so -3 goes before 3. Equal items cannot be told apart, so their order among themselves does not matter.

Do not change nums. An empty list gives an empty list.

Example 1

nums [3,-1,-3,2,0]
returns [0,-1,2,-3,3]

The absolute values are 0, 1, 2, 3, 3; the two 3s tie and the negative one goes first.

Example 2

nums [5,-5,5]
returns [-5,5,5]

-5 goes before the two 5s.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems