
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting String in Reverse Order in JavaScript
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";
Example
Following is the code −
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 .sort(sorter) .join(""); }; console.log(reverseSort(string));
Output
Following is the output in the console −
ollhe
Advertisements