← Back to challenges

Sort a String with the Given Template

JavaScriptHardalgorithmsconditionssortingstrings

Instructions

The function is given two strings t - template, s - to be sorted. Sort the characters in s such that if the character is present in t then it is sorted according to the order in t and other characters are sorted alphabetically after the ones found in t.

Examples

customSortt("edc", "abcdefzyx") ➞ "edcabfxyz"

customSortt("fby", "abcdefzyx") ➞ "fbyacdexz"

customSortt("", "abcdefzyx") ➞ "abcdefxyz"

customSortt("", "") ➞ ""

Notes

The characters in t and s are all lower-case.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.