Inspired by this Stack Overflow question: Sorting a list: numbers in ascending, letters in descending. Your task is to solve the following problem and, as this is code-golf, you should do so in as few as bytes as possible.
You should take a list of objects as input containing letters (any reasonable form: string
, char
, etc) and numbers. You should then sort the numbers into ascending order and the letters into descending order. However, you should keep letters in letter positions and numbers in number positions. For example, if the list is:
[L, D, L, L, D, L]
The output list should be in the form of:
[L, D, L, L, D, L]
Workthrough
Input: ['a', 2, 'b', 1, 'c', 3]
- Sort the numbers into ascending order:
[1, 2, 3]
- Sort the letters into descending order:
['c', 'b', 'a']
- Join them back but keep the order the same:
['c', 1', 'b', 2, 'a', 3]
Rules
- The list will only contain letters and digits.
- The list may be empty.
- The list may only contain letters or only digits.
- If your language does not support mixed type arrays you may use digit characters instead of numbers. Note that if your language does support this you must use mixed types.
- Letters will only be
[a-z]
or[A-Z]
, you may choose which one. - Letters are sorted as
a
being lowest,z
being highest i.e.a = 1, z = 26
. - Standard loopholes are forbidden.
- I/O may be by any standard means including as a string.
Test cases
[5, 'a', 'x', 3, 6, 'b'] -> [3, 'x', 'b', 5, 6, 'a']
[ 3, 2, 1] -> [ 1, 2, 3 ]
[ 'a', 'b', 'c' ] -> [ 'c', 'b', 'a' ]
[] -> []
[ 2, 3, 2, 1 ] -> [1, 2, 2, 3]
As this is code-golf the shortest answer in bytes wins!