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

Related... How to reverse a "string" and an "array" using while loop 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

You may also like... How to "split" a string into Multiple lines in a web page using JavaScript?

Happy coding. 🙂

← PreviousNext →