Home

SiteMap

How to sort a string in reverse order in JavaScript

← PrevNext →

You can use the .sort() and .reverse() methods together to sort a string in reverse order in JavaScript. These are built-it methods and its easy to use.

sort and reverse a string in javascript

See this example.

<script>
  const s_word = '58 45dr-P';
  const rev = [...s_word]
    .sort()	// sort the word (punctuations first, followed by numbers, uppercase followed by lowercase alphabets
    .reverse()	// reverse the sort order
    .join('');	// join the string (remove the commas)
  
    document.writeln(rev);      // Output: rdP8554-
</script>
Try it

Happy coding. 🙂

← PreviousNext →