This challenge is inspired by this very nice answer by TidB.

In TidB's answer, every eight character is in the correct order: gnilwoB edoC (Code Bowling backwards). The other strings however are were in a strange, random order.

Your challenge is to fix this.

Take a (non-empty) string and a positive integer n as input. The string will contain ASCII characters in the range: 32-126 (space to tilde).

You must sort the string in ascending order (seen from the left, based on the ASCII-code value), but skip every nth character, starting from the end of the string. As an example, let's take the string abcdABC123 as input, and n=4, then we'll get:

abcdABC123   <- Input string. (n=4)
_b___B___3   <- These will not be sorted (every 4th starting from the end)
1_2AC_acd_   <- The remaining characters, sorted
1b2ACBacd3   <- The final string (the output)

Another example:

9876543210   <- Input string (n=2)
_8_6_4_2_0   <- These will not be sorted
1_3_5_7_9_   <- The remaining characters, sorted
1836547290   <- The final string (the output)

The input string can be taken on an optional format (string, list of characters, list of single character strings ...). The input integer can also be taken on an optional format.

Test cases:

The format will be n=__, followed by the input string on the next line. The output is on the line below.

n=1   (All elements will stay in place)

n=214  (The last character will stay in place. All other are sorted. 

g7L9T E^n I{><#ki XSj!uhl y= N+|wA}Y~Gm&o?'cZPD2Ba,RFJs% V5U.W;1e  0_zM/d$bH`@vKoQ 43Oq*C
g       n !#$%&'i*+,./01l234579;w<=>?@ADoEFGHIJKBLMNOPQR STUVWXYeZ^_`abcdhjkmqsuovyz{|}~C

19 Answers 19


MATL, 15 14 bytes


Inputs are a string enclosed in single quotes and a number. Single-quote symbols in the string should be escaped by duplicating (as in MATLAB and Octave).

Try it online! Or verify all test cases.


Consider inputs 'abcdABC123' and 4.

tt     % Implicitly input string. Duplicate twice
       % STACK: 'abcdABC123', 'abcdABC123', 'abcdABC123'
f      % Find: indices of nonzero elements: gives [1 2 ... n] where n is input length
       % STACK: 'abcdABC123', 'abcdABC123', [1 2 3 4 5 6 7 8 9 10]
i      % Input n
       % STACK: 'abcdABC123', 'abcdABC123', [1 2 3 4 5 6 7 8 9 10], 4
X\     % 1-based modulo
       % STACK: 'abcdABC123', 'abcdABC123', [1 2 3 4 1 2 3 4 1 2 3 4]
qg     % Subtract 1, convert to logical: gives true (1) for 1, false (0) otherwise
       % STACK: 'abcdABC123', 'abcdABC123', [0 1 1 1 0 1 1 1 0 1]
P      % Flip
       % STACK: 'abcdABC123', 'abcdABC123', [1 0 1 1 1 0 1 1 1 0]
)      % Use as logical index into the string
       % STACK: 'abcdABC123', 'acdAC12'
S      % Sort
       % STACK: 'abcdABC123', '12ACacd'
5M     % Push logical index again
       % STACK: 'abcdABC123', '12ACacd', [1 0 1 1 1 0 1 1 1 0]
(      % Write into original string as specified by the index. Implicitly display
       % STACK: 1b2ACBacd3

1-based modulo means that mod([1 2 3 4 5], 3) gives [1 2 3 1 2] instead of the usual (0-based) [1 2 0 1 2]. This is needed here to handle the case n=1 adequately.

  • 2
    \$\begingroup\$ I wish 05AB1E had that last command... \$\endgroup\$
    – mbomb007
    Commented Mar 15, 2017 at 20:55

PHP, 101 bytes

negative string indexes (PHP 7.1) save 21 bytes - and possibly the day:


Run with php -nr '<code>' '<string>' <N>.


for([,$s,$n]=$argv;     # import command line arguments to $s and $n
    a&$c=$s[$i-=1];)    # loop backward through string
    $i%$n+1?$a[]=$c:0;      # if index is not n-th, copy character to array
for(sort($a);           # sort array
    ++$i;)              # loop forward through string:
    echo$i%$n+1             # if index is not n-th
        ?$a[+$k++]              # print character from array
        :$s[$i]                 # else print character from string
  • \$\begingroup\$ why $i-=1 and not $i-- ? \$\endgroup\$ Commented Mar 15, 2017 at 16:26
  • 1
    \$\begingroup\$ @JörgHülsermann Because $i-- doesn´t work if $i is NULL. \$\endgroup\$
    – Titus
    Commented Mar 15, 2017 at 18:18
  • \$\begingroup\$ @JörgHülsermann ... and --$i, which I would need does also not. ;) \$\endgroup\$
    – Titus
    Commented Mar 15, 2017 at 18:47
  • \$\begingroup\$ I have never tried it before. Thank You for your answer \$\endgroup\$ Commented Mar 15, 2017 at 18:48

Python 2, 191 bytes

Yeah, I'm sure this is a terrible solution.

t=J(sorted(J(s[i*n+1:i*n+n]for i in R)))
print J(j[::-1]for i in zip(k,[t[::-1][i*n:i*n+n][::-1]for i in R])for j in i)[::-1]

Try it online

I'm not going to bother explaining it. It was alright until I realized that it needs to be indexed from the end. Now it's a monster. At this point, I'm just glad it works.

  • 1
    \$\begingroup\$ Upvoted because of the "explanation". :P \$\endgroup\$ Commented Mar 15, 2017 at 20:24

Octave, 65 54 bytes

function s=f(s,n)

Try it online!

Uses logical indexing to make an array of 'fixed' and 'sorted' characters. Explanation:

function s=f(s,n) % Create a function, taking a string `s` and the number `n`; the output is also named `s`.
l=~~s;             % Create logical array with the same size of the input string 
                  %    [equivalent to much more verbose true(size(s))].
l(end:-n:1)=0;    % Set the 'fixed' character positions. MATLAB/Octave automatically produces
                  %    the correct result even if n is larger than the string length.
s(l)=sort(s(l)) % Select the elements from `s` where `l` is true. Sort, and store in the corresponding positions in `s`.

The way I created l requires that s is nonzero, which I think is a reasonable requirement, as many languages use \0 as an end-of-string delimiter.

  • \$\begingroup\$ You can save some bytes if you bypass l and use a vector of index numbers directly \$\endgroup\$
    – Leo
    Commented Mar 15, 2017 at 19:29
  • \$\begingroup\$ @Leo meta.codegolf.stackexchange.com/questions/11710/… \$\endgroup\$
    – Sanchises
    Commented Mar 15, 2017 at 19:46
  • \$\begingroup\$ @Leo, isn't your suggestion 8 bytes longer? \$\endgroup\$ Commented Mar 15, 2017 at 20:22
  • \$\begingroup\$ @StewieGriffin whoops, I didn't see the updated solution \$\endgroup\$
    – Leo
    Commented Mar 15, 2017 at 21:55

JavaScript (ES6), 100 93 bytes

Takes input in currying syntax (s)(n).


Formatted and commented

s => n => s.replace(        // given a string s and an integer n
  /./g,                     // for each character c of s
  (c, i) => (               // at position i:
    F = _ =>                //   F = function that tests whether the
      (s.length - ++i) % n  //       character at position i is non-static
  )()                       //   call F() on the current position
  ?                         //   if the current character is non-static:
    [...s].filter(F, i = 0) //     get the list of non-static characters
      F, i = 0              //     by filtering all characters in s with F()
    )                       //
    .sort()[j++]            //     sort them and pick the next occurrence
  :                         //   else:
    c,                      //     let c unchanged
  j = 0                     //   initialize j = non-static character pointer
)                           //

Test cases

let f =


console.log(f("g7L9T E^n I{><#ki XSj!uhl y= N+|wA}Y~Gm&o?'cZPD2Ba,RFJs% V5U.W;1e  0_zM/d$bH`@vKoQ 43Oq*C")(8))


Perl 5 -lF, 78+3 = 81 bytes

$i=<>;map{(@F-++$c)%$i&&((push@b,$_),$_='')}@F;say map/./?$_:(sort@b)[$g++],@F

Try it online!

  • \$\begingroup\$ Nice answer! I think this is a slightly different approach, but I can't be sure so I'll share here as a comment. I think that even with old scoring it's -1: Try it online! \$\endgroup\$ Commented Dec 11, 2023 at 9:58

Jelly, 14  13 bytes


Full program that prints the string to STD out*.

Try it online!


ṚsṚµḢ€ż@Ç - Main link: string s, non-negative number n
Ṛ         - reverse s
 s        - split into chunks of size n
  Ṛ       - reverse the resulting list
   µ      - monadic chain separation (call that list x)
    Ḣ€    - head €ach - yield a list of the first entries of each of x and modify x
        Ç - call the last link (1) as a monad - get the sorted and re-split list
      ż@  - zip together (with reversed @rguments)

FṢṁ - link 1, sort and reshape like self: list of lists
F   - flatten into a single list
 Ṣ  - sort
  ṁ - mould the result like the input

I can't help but think there is a way to use the fact that modifies its input

* for a function one would want to flatten the output into a single list with F.
For example an input of "abcdABC123", 4 yields:
rather than:


JavaScript (Node.js), 100 bytes


Attempt This Online!

Use no sort


Perl 5, 94 bytes

88 bytes of code + -F -pl flags.

$_=join"",(map{(--$i%$n?"":$F[$#F-$i--]),$_}sort grep$i++%$n,reverse@F),chop if($n=<>)>1

Try it online!

It's quite too long in my opinion, but already not that ugly... I'm still trying to golf it further anyway.


Python + NumPy, 115 114 bytes

from numpy import *
def f(x,n):l=len(x);x=array(x);m=[1<2]*l;m[-1::-n]=[1>2]*len(m[0::n]);x[m]=sort(x[m]);return x

Takes a regular Python list as input (wasn't sure whether taking an array would be considered kosher); returns a NumPy array containing the result.

Works by masking out the relevant indices and sorting the rest.


Python 2, 119 113 bytes

print"".join(sorted(l[~a]for a in i if a%n)[-a+a/n]if a%n else l[~a]for a in i)[::-1]

Builds a list of all characters to be sorted, sorts them and merges them for printing, while avoiding some of the reversing via negative indexing.

  • 1
    \$\begingroup\$ print"".join(sorted(l[~a]for a in i if a%n)[-a+a/n]if a%n else l[~a]for a in i)[::-1] saves 5 bytes \$\endgroup\$
    – TidB
    Commented Mar 16, 2017 at 9:26
  • \$\begingroup\$ @TidB Thanks, almost eliminated the scrollbar! (Apparantly there was a trailing newline involved in my previous count, therefore it seems to be 113 now instead of 114.) \$\endgroup\$
    – moooeeeep
    Commented Mar 16, 2017 at 10:25

Burlesque, 36 bytes


Try it online!

Takes input as N "String"

pe       # Parse and push
<-       # Reverse the string
j        # Put N at the top of the stack
J-.hd    # Store N-1 for later
co       # Split string into chunks of N
J)-]     # Duplicate and take the heads
j)[-     # Take the tails of the other duplicate
++       # Reduce all the "useless" chars to string
<>       # Reverse sort
#aco     # Split into chunks of N-1
z[       # Zip the two lists
\[\[     # Flatten to string
<-       # Reverse the string

Perl 6, 53 bytes

{(my@a=$^a.comb)[grep (@a-*-1)%$^n,^@a].=sort;[~] @a}

Try it online!

It feels like there should be a shorter way of doing this


05AB1E, 14 bytes


I/O of the string as a list of characters.

Try it online or verify all test cases (feel free to remove the S and J in the test suite to see the actual I/O with character lists).

And I agree with @mbomb007's comment, although it's still pretty short this way. :)


                #  Example inputs: s=["a","b","c","d","A","B","C","1","2","3"] and n=4
D               # Duplicate the (implicit) character-list input
                #  STACK: [["a","b","c","d","A","B","C","1","2","3"],
                #          ["a","b","c","d","A","B","C","1","2","3"]]
 ā              # Push a list in the range [1,list-length] (without popping the list)
                #  STACK: [s, s, [1,2,3,4,5,6,7,8,9,10]]
  R             # Reverse it to range [length,1]
                #  STACK: [s, s, [10,9,8,7,6,5,4,3,2,1]]
   <            # Decrease each by 1 to range (length,0]
                #  STACK: [s, s, [9,8,7,6,5,4,3,2,1,0]]
    IÖ          # Check for each if it's divisible by the integer-input
                #  STACK: [s, s, [0,1,0,0,0,1,0,0,0,1]]
      ≠         # And invert those truthy/falsey values
                #  STACK: [s, s, [1,0,1,1,1,0,1,1,1,0]]
       ©        # Store this list of truthy/falsey values in variable `®`
        Ï       # Only keep the characters in the list at the truthy indices
                #  STACK: [s, ["a","c","d","A","C","1","2"]]
         {      # Sort those
                #  STACK: [s, ["1","2","A","C","a","c","d"]]
          ®     # Push the truthy/falsey list again from variable `®`
                #  STACK: [s, ["1","2","A","C","a","c","d"], [1,0,1,1,1,0,1,1,1,0]]
           ƶ    # Multiply each by their 1-based index
                #  STACK: [s, ["1","2","A","C","a","c","d"], [1,0,3,4,5,0,7,8,9,0]]
            <   # Decrease everything by 1 to make it 0-based indexing
                #  STACK: [s, ["1","2","A","C","a","c","d"], [0,-1,2,3,4,-1,6,7,8,-1]]
             ǝ  # Insert the sorted characters at those indices in the list
                # (NOTE: Unlike almost all other 05AB1E builtins that use indices, this
                # insert builtin ignores negative and too large indices instead of using
                # modular indexing, so the -1 are conveniently ignored here.)
                #  STACK: [["1","b","A","C","a","B","d","1","2","3"]]
                # (after which this list is output implicitly as result)

Uiua, 16 bytes


Try it!

             ⇡⧻,  # range of length
           ◿:     # modulo input
         =0       # where is it zero?
        ⇌         # reverse
       ¬          # not
⍜▽(⊏⍏.)           # under-keep-sort (sort input where mask is 1)

J, 28 bytes


Try it online!


Vyxal, 11 bytes


Try it Online!

Outputs as a list of characters


  ~Ḟ         # ‎⁡Every nth item of
ẏṘ           # ‎⁢The range [len(string), 0]
  ~          # ‎⁣without popping anything
    F        # ‎⁤Remove each item in that "every nth item" list from the range
     Ṙ       # ‎⁢⁡Reverse that so that the indices are in ascending order
          ¢  # ‎⁢⁢Apply
      ⁽s     # ‎⁢⁣A function that sorts its argument
         ∇   # ‎⁢⁤to the items at indices in
        ?    # ‎⁣⁡the string
          ¢  # ‎⁣⁢and insert the items in the result back at those indices

Created with the help of Luminespire.


Ruby, 64 bytes

Uses regex to grab all irrelevant characters, both for replacement and for sorting.


Try it online


Python 3.8 (pre-release), 111 109 bytes

lambda n,s:(l:=len(s),r:=sorted(s[~i]for i in range(l)if i%n),[r.insert(i,s[i])for i in range(~-l%n,l,n)])[1]

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.