Simple String Sort Function

SunJet Liu
Jul 6, 2021

--

/*
Simple String Sort Function
Given two arguments, a string of characters (input) and a sort string (sortStr),
implement a simple sort function that returns the characters to sort in the order
specified by the sort string. If any characters do not appear in the sort string,
they go at the end, in any order.
@Example
sort(‘banana’, ‘nab’) -> ‘nnaaab’
sort(‘house’, ‘soup’) -> ‘souhe’ or ‘soueh’
*/

function sort(input, sortStr) {
let sorted =[]
let remainder = input
for(let i =0; i<=sortStr.length -1; i++){
for(let j =0; j<=input.length; j++){
if (sortStr[i] == input[j]){
sorted.push(sortStr[i])
console.log(sorted)
}
}
}
remainder = remainder.split(“”)
let end =remainder.filter( x => !sorted.includes(x))
sorted.push(end.join(“”))
return sorted.join(“”)
}

sort(‘house’, ‘soup’)
sort(‘banana’, ‘nab’)

--

--