以相反顺序对 JavaScript 中的字符串排序
我们要编写一个 JavaScript 函数,该函数采用一个小写字符串,并按相反的顺序对它进行排序,即,b 应在 a 之前,c 在 b 之前,依次类推。
例如:如果输入字符串为 −
const str = "hello";
那么输出应该是 −
const output = "ollhe";
示例
以下是代码 −
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));输出
以下是控制台中的输出 −
ollhe
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP