← back to unit
Medium
# sorting medium
Sort by Distance from Zero
The space station lists nearby objects by how far they are — direction doesn't matter, minus signs are just directions. 🛰️
You get a list of nums. Sort them by distance from zero, closest first (ties keep original order), and print comma-separated.
Example:
Input: nums = [-7, 2, -3, 10]
Output: 2,-3,-7,10
Distances from zero are 7, 2, 3, 10. Sorting by key=abs gives closest-first: 2 (dist 2), -3 (dist 3), -7 (dist 7), 10 (dist 10).
💡 need a hint?
sort-distance-from-zero.py🔒 given lines are locked — write your code in between
loading...