在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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短