[Question]
  [
Given a ragged list, e.g.
```
[[4, 7], [5, 3, [], [6, [2]]]]
```
Your challenge is to sort only the numbers in it. For example, with the above, the result would be
```
[[2, 3], [4, 5, [], [6, [7]]]]
```
The shape of the output should remain the same, only the numeric contents should change.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), shortest code wins!
All numbers will be unique positive integers. You may do input / output with strings.
## Testcases
```
[2, [3, [1, []]]] -> [1, [2, [3, []]]]
[6, 5, [[9]], 7] -> [5, 6, [[7]], 9]
[12, [43, 51], [[23, 4], 9, [22, []]]] -> [4, [9, 12], [[22, 23], 43, [51, []]]]
[9, 4, 2] -> [2, 4, 9]
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
```
FṢṁ
```
[Try it online!](https://tio.run/##y0rNyan8/9/t4c5FD3c2/j/cfnTSw50zNCP//4/mio420VEwj9VRiDbVUTAGUiCmGRAbxQKBDle0EZANEjcEyUGEgNJAxdHRlkAeSC9XtCFIlQlQmakhSH@0EZBpAmRZggwyQugE8oHWGQGZsQA "Jelly – Try It Online")
## How it works
```
FṢṁ - Main link. Takes a ragged list R on the left
F   - Flatten R
 Ṣ  - Sort the flattened R
  ṁ - Mold the sorted flattened R into the same shape as R
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~51~~ 47 bytes
```
->l{eval"#{l}".gsub(/\d+/,'%d')%l.flatten.sort}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6nOrUsMUdJuTqnVkkvvbg0SUM/JkVbX0ddNUVdUzVHLy0nsaQkNU@vOL@opPZ/gUJadHS0iY6CeayOQrSpjoIxkAIxzYDYKBYE/gMA "Ruby – Try It Online")
-4 bytes thanks to Sisyphus
[Answer]
# [R](https://www.r-project.org), 29 bytes
```
\(l)relist(sort(unlist(l)),l)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dZFNCsIwEIVx6ylCVwmki0z_LIoXmXYhYkEoFfpzBE_hpoIew4t4GmeaBtuiWSTD65tv3tDbve4fhdj5z64t_M0rk6WqT-W5aWVzqVvZVUNdKqVLZT3v1bWQVuUr1CJRWgx1pEUwlk6KxxcUn-3x0EpP-HuBCGTOtUACUB9ySWZMcjpZ5an1OAVGgiMbN2EBJJ2ZGDBswYiHGd_QKe1Dsaft9J3H83wt0lm3cRFCYkdGTUlAUkhK6vaEP_FoTSSTAV4U2Qa8PiMxMj8yk5l6YAaBQbPp7N_oe_t-AA)
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 34 bytes
```
0#/.(i=0):>Sort[Flatten@#][[++i]]&
```
[Try it online!](https://tio.run/##PYzBCoMwEETv/oYgLU5bE6NiQemp54LH4CGIYqBakNyW@OvpFqF72Z03M7sYN4@LcXYwYWpCFt@uJ9tk53vbfTann2/j3Lg@4l7rNLV9n4TXZlen40s7MU32bjDrThGRQuVBBXIQ7xIkPQ8ikiBmgvGhSxQgqvmuflKwr3IUglskcyiPmsvyn6@hII9HEsJHPnwB "Wolfram Language (Mathematica) – Try It Online")
Also works if input numbers are not unique.
```
0#                                  numbers become 0
  /.   0 :>                         replace 0s (in order) with:
    (i= )  Sort[Flatten@#][[++i]]     the corresponding sorted value
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 32 bytes
-2 bytes thanks to @att.
```
#/.Thread[#->Sort@#&@Flatten@#]&
```
[Try it online!](https://tio.run/##RYvBCsMgEER/ZWHBk2nQmpYcGjz1XGhv4kFaQwJJCrI38dvteurCMPOWmT3QEvdA6zvUGW4V@9NrSTF8HHbT85vIorD3LRDFw6IX9ZHWgxxCN8Hs0HsQ0FvIWUvIZ5ZiFT62i4SBLY@Nru2jWstwbVANs@ZoOI0MWv@XzEaCLqX@AA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 5 bytes
```
N`\d+
```
[Try it online.](https://tio.run/##K0otycxLNPyvquGe8N8vISZF@///aCOdaGOdaEOd6Fgg4Io20zHViY62jI3VMQfyDIGyJsY6poaxQEEjYx2TWB1LnWgjI5hqSx0THaNYAA)
**Explanation:**
Sorts all numbers of 1 or more digits in the input in numerical order, and leaves every other character unchanged (including their positions).
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 62 bytes
```
n=>n.replace(r=/\d+/g,_=>n.match(r).sort((a,b)=>a-b)[i++],i=0)
```
[Try it online!](https://tio.run/##HYzhDoIgHMRfhW/A/KdJVnMNX4RYQ0SjGTikXp@g@3R3v9u91FftOtgtHpyfTJp5cnxwdTDbqrQhgTf3qWoWeJT2raJ@kkDr3YdIiIKR8kEdRipsVUmw/EjT7AMi2rs9Iuu2T0R@RgKLC6AzICF6KQFdJQYsWpaL7pRBKwti2XbZ9TmwwmRWGfbQAZNYUlR@/Wrq1S9kJv9/Sm/pBw "JavaScript (Node.js) – Try It Online")
Probably defeats the purpose of the challenge, but it is valid. Takes in a stringified list and outputs a stringified list. Replaces the `i`-th number (via string replacement) with the `i`-th element of the list of sorted numbers.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 19 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
"DdiI˜{¾è¼ë®δ.V"©.V
```
Unfortunately, a simple flatten and replace/transliterate doesn't work on nested lists apparently. So instead, the default recursive approach I've used in multiple other [ragged-list](/questions/tagged/ragged-list "show questions tagged 'ragged-list'") challenges have been used.
[Try it online](https://tio.run/##yy9OTMpM/f9fySUl0/P0nOpD@w6vOLTn8OpD685t0QtTOrRSL@z//2hDIx2FaBNjHQVTw1ggK9oIyDQBsiyBHCOQXCwQAAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfaVL6H8ll5TMiNNzqg/tO7zi0J7Dqw@tO7dFL0zp0Eq9sP86eoe2/o@ONtKJNtaJNtSJjgUCHYVoMx1TnehoSyDbHMQ1BMqbGOuYGoI40UbGOiaxOpY60UZGcA2WOiY6RrGxAA).
**Explanation:**
```
"..."     # Create the recursive string explained below
     ©    # Store it in variable `®` (without popping)
      .V  # Evaluate and execute it as 05AB1E code
          # (after which the result is output implicitly)
D         # Duplicate the current item
          # (which will be the implicit input-list in the first iteration) 
 di       # If it's a (non-negative) integer:
   I      #  Push the input-list
    ˜     #  Flatten it
     {    #  Sort it
      ¾è  #  Get the `¾`'th value of this sorted list
        ¼ #  Then increase `¾` by 1
  ë       # Else (it's a list instead):
    δ     #  Map over each inner list:
   ® .V   #   Do a recursive call for each
```
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 89 bytes
```
def f(L):a=re.split('(\d+)',L);a[1::2]=sorted(a[1::2],key=int);return''.join(a)
import re
```
[Try it online!](https://tio.run/##TU1NboQgFN5zipfZCCljAurM6MSeYG5AWZiIKa0FA3Th6e1jtE1JXvL98i1reveuui1h20YzwUQfrBv6YMq4zDbRgr6NL6zgD3YflOg6qfvoQzIjPSj/NGtvXWL3YNJ3cEVRfnjr6MCI/VowCsFsB4prJJMPMIN1mZQxjdaVwQzjbJ2JlHUELPfQw3zsn@D8CidGYAk4Qi2fqGWMbEpyUBWewNP4cu5Jfo0sEnXh0CBRrdYcrnsKhUvWrllrMSRyp8ZSI3Q2JMI6e/k7@X@gRoKqkHsOPVkhzF3ViL9VjGBS7h35JK3@AQ "Python 3.8 (pre-release) – Try It Online")
I/O are strings.
## How
This simply ignores the nesting by picking out groups of digits sorting them and putting them back into the gaps.
[Answer]
# [Factor](https://factorcode.org/) + `sorting.human`, 93 bytes
```
[ find-numbers [ string? ] partition natural-sort [ present ] map 2array round-robin ""join ]
```
[Try it online!](https://tio.run/##TY2xCsJAEER7v2JIHVMIKmphKTY2YnVcscaNniZ7ce8O9OvjKQpWwzCPeQ3V0etw2G93myWC1@jkXF1SR4IQNZeAwPfEUnP47eiVA0sEqdLzD6j4EZUCnMeNVbjFajQqzKyclsYsrC3nthgMGiensaTuyBpgvp41LHrK/9F5gVBMSu34bczIT2jRUY/Jxwv1Kf@oPzpBUVx9DjvU1LYZdxKHFw "Factor – Try It Online")
## Explanation
```
                       ! "[6,5,[[9]],7]"
find-numbers           ! { "[" 6 "," 5 ",[[" 9 "]]," 7 "]" }
[ string? ] partition  ! { "[" "," ",[[" "]]," "]" } { 6 5 9 7 }
natural-sort           ! { "[" "," ",[[" "]]," "]" } { 5 6 7 9 }
[ present ] map        ! { "[" "," ",[[" "]]," "]" } { "5" "6" "7" "9" }
2array                 ! { { "[" "," ",[[" "]]," "]" } { "5" "6" "7" "9" } }
round-robin            ! { "[" "5" "," "6" ",[[" "7" "]]," "9" "]" }
""join                 ! "[5,6,[[7]],9]"
```
Here's a slightly longer version that works on native sequences:
```
[ [ flatten natural-sort ] keep 0 -rot [ dup real? [ drop 2dup nth [ 1 + ] 2dip ] when ] deep-map ]
```
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), 19 bytes [SBCS](https://github.com/abrudz/SBCS)
```
∊{a⊣(∊a)←⍺[⍋∊a←⍵]}⊢
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=AwA&c=e9TRVZ34qGuxxqOOrkTNR20THvXuin7U2w3ignlbY2sfdS0CAA&f=S1N41DfVK9jfTz3aSEch2hiIDYE4FggU1LnSELJmOgqmQIloy9hYHQVzNElDkF4ToGZTw1iQIiMg0wTIsgRyjIywmgeUMtFRMAKJAgA&i=AwA&r=tryapl&l=apl-dyalog&m=train&n=f)
A tacit function which takes a ragged array.
# Explanation
```
∊{a⊣(∊a)←⍺[⍋∊a←⍵]}⊢ Pass in:
                  ⊢ The input as is on the right
∊                   The flattened input on the left.
 {a⊣(∊a)←⍺[⍋∊a←⍵]}  and do the following:
            a←⍵     assign right arg to 'a'
         ⍺[⍋∊   ]   sort the flattened input
    (∊a)←           assign this result to 'a', preserving structure
  a⊣                return modified 'a'
```
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 37 bytes
```
@a=sort{$a-$b}/\d+/g;s/\d+/shift@a/ge
```
[Try it online!](https://tio.run/##K0gtyjH9/98h0bY4v6ikWiVRVyWpVj8mRVs/3boYTBdnZKaVOCTqp6f@/x9taKSjEG1irKNgahgLZEUbAZkmQJYlkGMEkosFgn/5BSWZ@XnF/3V9TfUMDA3@6xYAAA "Perl 5 – Try It Online")
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 38 bytes
```
1N→_a`\d+`?λ←_a1+→_a`\d+`?Ẏ⌊s←_a iS;øṙ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCIxTuKGkl9hYFxcZCtgP8674oaQX2ExK+KGkl9hYFxcZCtgP+G6juKMinPihpBfYSBpUzvDuOG5mSIsIiIsImBbMTIsIFs0MywgNTFdLCBbWzIzLCA0XSwgOSwgWzIyLCBbXV1dXWAiXQ==)
Thanks @lyxal for helping me a lot in chat. I've never posted any Vyxal answers before.
This is a port of my JavaScript solution. Takes a stringified list (provided with backticks around a Python-like list syntax).
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), 86 bytes
```
a->i=#l=[];t(b->l=setunion([b],l),a);t(b->l[i++],a)
t(g,a)=iferr([t(g,b)|b<-a],e,g(a))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=RY7RCsIgFIbvewpZN8r0YrZVo9xjdCMSDuYQZIm5i6I36WYQ0TP1Nh1rkHD8_8_zn4P3l9fBHns_PQwSzzEatn0fNGusWDoh1S7iljVOnLs4DvY0YNkq6gjVZO5Im-cKcBFxDyKs6ULAMlFLbu2eaUU72mNNyLz9qr13F6wRa5APdohgswQZMilGkZQcrhVUAaXggKwpqlKrTrRJL0VKlRCrCvUdAluCqwE4_08ClxRxpeYfTNNPPw)
## Explanation
Here is a helper function to traverse a ragged-list, since we need to traverse the input twice:
```
t(g,a)=iferr([t(g,b)|b<-a],e,g(a))
```
`t(g,a)` traverses the ragged-list `a` and replaces each number in it with `g(a)`.
When `a` is a number, the list comprehension `[t(g,b)|b<-a]` will throws an error. `iferr` will catch this error and return `g(a)`.
When `a` is a list, `[t(g,b)|b<-a]` doesn't have an error. It returns a list of `t(g,b)`, where `b` runs through `a`.
Now look at the main function:
```
a->i=#l=[];t(b->l=setunion([b],l),a);t(b->l[i++],a)
```
This is an anonymous function with argument `a`.
First it initializes `l` to `[]`, `i` to `#l` (the length of `l`, which is `0`).
Now it can do the first traversal: `t(b->l=setunion([b],l),a)`. For each number `b` in the list, it set `l` to the union of `[b]` and `l`. PARI/GP doesn't have a separated type for sets. A set is just a sorted list without duplicate elements.
Then it does the second traversal: `t(b->l[i++],a)`. For each number `b` in the list, it increments `i`, and replaces `b` with the `i`'th element in `l`. This gives the final result.
]