Challenge:
Given a list of integer, sort descending by their single largest digit(s). The order for numbers with the same largest digit are then sorted by second largest digit, etc.
We ignore duplicated digits in numbers. And if all digits in a number are the same, the order of those numbers in the list can be in any way you'd like.
Example:
Input: [123, 478, -904, 62778, 0, -73, 8491, 3120, 6458, -7738, 373]
Possible outputs: [8491, -904, 62778, 478, -7738, 6458, 373, -73, 3120, 123, 0]
[8491, -904, 62778, 478, -7738, 6458, -73, 373, 3120, 123, 0]
Why? Here are the relevant digits the numbers were sorted on:
Output:
[8491, -904, 62778, 478, -7738, 6458, 373, -73, 3120, 123, 0 ]
Relevant digits they were sorted on:
[[9,8], [9,4], [8,7,6], [8,7,4], [8,7,3], [8,6], [7,3], [7,3], [3,2,1,0], [3,2,1], [0]]
Challenge rules:
- We ignore duplicated digits, so
478
and-7738
will be ordered as478, -7738
, because the largest digits are[8,7,4]
and[8,7,3]
, and not[8,7,4]
and[8,7,7,3]
. - If multiple numbers have the same digits, the order of those can be either way. So
373
and-73
can be sorted as both373, -73
or-73, 373
(digits are[7,3]
for both of these numbers). - If a number contains no more digits to check, it will be placed at the back of the relevant numbers. So
123
and3120
will be sorted as3120, 123
, because the largest digits[3,2,1]
are the same, but0
comes beforenone
. - You can assume all numbers in the input are in the range
[-999999,999999]
. - Just one of the possible outputs is enough as result, but you are allowed to output all possible outputs where sublists can be in any permutation if you want (although I doubt it would save bytes in any language).
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input: [123, 478, -904, 62778, 0, -73, 8491, 3120, 6458, -7738, 373]
Possible outputs: [8491, -904, 62778, 478, -7738, 6458, 373, -73, 3120, 123, 0]
[8491, -904, 62778, 478, -7738, 6458, -73, 373, 3120, 123, 0]
Input: [11, -312, 902, 23, 321, 2132, 34202, -34, -382]
Possible outputs: [902, -382, 34202, -34, -312, 321, 2132, 23, 11]
[902, -382, 34202, -34, 2132, -312, 321, 23, 11]
etc. The sublist [-312, 321, 2132] can be in any permutation
Input: [9, 44, 2212, 4, 6, 6, 1, 2, 192, 21, 29384, 0]
Possible outputs: [29384, 192, 9, 6, 6, 4, 44, 2212, 21, 2, 1, 0]
[29384, 192, 9, 6, 6, 44, 4, 2212, 21, 2, 1, 0]
etc. The sublists [4, 44] and [2212, 21] can be in any permutation
Input: [44, -88, 9, 233, -3, 14, 101, 77, 555, 67]
Output: [9, -88, 67, 77, 555, 14, 44, 233, -3, 101]