← Back to challenges

Sort by Digit Length, then By Number Itself

JavaScriptHardsortingarrays

Instructions

Write a function that sorts an array of integers by their digit length in descending order, then settles ties by sorting numbers with the same digit length in ascending order.

Examples

digitSort([77, 23, 5, 7, 101])
➞ [101, 23, 77, 5, 7]

digitSort([1, 5, 9, 2, 789, 563, 444])
➞ [444, 563, 789, 1, 2, 5, 9]

digitSort([53219, 3772, 564, 32, 1])
➞ [53219, 3772, 564, 32, 1]

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.