按字母倒序排序 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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP