Skip to main content
1 of 7
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

Post-determined Array Sorting

Input:

  • An array containing three integers: 0, 1 and 2 in any order (i.e. [2, 0, 1])
  • And a string of length >= 2 only containing alphabetic letters (both lower- and uppercase) and digits (i.e. a1B2c3)

Output:

Based on the array we sort and output the string.
How does this work?

  • The array indicates the order precedence of a-z, A-Z and 0-9, the first being 0; second being 1; and third being 2.
  • The individual characters of the string can then be ordered based on that.

Example:

  • Array: [2, 0, 1]
  • String: a1B2c3

Based on the array, we know our order precedence is 0-9a-zA-Z.
Based on that, we can convert and output the string: 123acB.

Challenge rules:

  • For the array you can choose to use 0-indexed or 1-indexed input, so [3, 1, 2] is also a valid input if you prefer to use 1-indexed arrays.
  • The string (both input and output) only contains valid characters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.
  • If your language doesn't support arrays (or if you choose to), you are free to use strings resembling arrays in the form of [0,1,2] for the first parameter.

General rules:

  • This is , 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, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

[2, 0, 1] & a1B2c3 -> 123acB [2, 1, 0] & aAaA909UuHWw9gh2 -> 02999AAHUWaaghuw [2, 1, 0] & 6Bx43 -> 346Bx [1, 0, 2] & jfjf33g -> ffgjj33 [0, 2, 1] & AbC13 -> b13AC [1, 2, 0] & Qfl0l -> Q0fll [0, 1, 2] & 9870abcABC -> abcABC0789 [0, 2, 1] & test123 -> estt123 [2, 0, 1] & WHAT -> AHTW [2, 0, 1] & WhAt -> htAW [1, 0, 2] & 102BACbac -> ABCabc012 
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394