首页 > 解决方案 > 在 JavaScript 中按索引大小排序

问题描述

如何根据索引大小对字符串进行排序?

帮助排序的方法还是没有任何方法?

例如:

Input : sky is blue today
Output: is sky blue today 

标签: javascript

解决方案


您想按每个单词的长度排序吗?

var str = "sky is blue today"

console.log(
  str.split(" ").sort(function(a, b) { return a.length - b.length; }).join(" ")
)


推荐阅读