以字符串的升序排列单词-JavaScript
假设我们需要编写一个JavaScript函数,该函数接受一个字符串并返回一个新字符串,并根据单词的长度增加对其进行重新排列。
示例
以下是代码-
const str = 'This is a sample string only';
const arrangeByLength = str => {
const strArr = str.split(' ');
const sorted = strArr.sort((a, b) => {
return a.length - b.length;
});
return sorted.join(' ');
};
console.log(arrangeByLength(str));输出结果
以下是控制台中的输出-
a is This only sample string
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短