Était-ce utile?


Reverse alphabetically sorted strings in JavaScript

JavascriptWeb DevelopmentObject Oriented ProgrammingFront End Technology

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.

For example:

If the input string is −

const str = "hello";

Then the output should be −

const output = "ollhe";

The code for this will be −

const string = 'hello';
const sorter = (a, b) => {
   const legend = [-1, 0, 1];
   return legend[+(a < b)];
const reverseSort = str => {
   const strArr = str.split("");
   return strArr

Following is the output on console −

Published on 10-Oct-2020 10:59:15
Était-ce utile?
Non affilié à Tutorialspoint
scroll top