
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Reverse alphabetically sorted strings 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";
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 .sort(sorter) .join(""); }; console.log(reverseSort(string));
Following is the output on console −
ollhe
- Related Questions & Answers
- Python – Extract Sorted Strings
- Order items alphabetically apart from certain words JavaScript
- Sorting string alphabetically and inserting underscores using JavaScript
- C++ program to concatenate strings in reverse order
- C Program to Reverse Array of Strings
- Program to count sorted vowel strings in Python
- Array reverse() in JavaScript
- JavaScript Array reverse()
- Reverse numbers in function without using reverse() method in JavaScript
- Reverse a number in JavaScript
- Reverse sum array JavaScript
- Formatted Strings Using Template Strings in JavaScript
- Python program to count the pairs of reverse strings
- Template strings in JavaScript.
- Reverse mapping an object in JavaScript
Advertisements