Simple String Sort Function

/*
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’)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store