js数组 按照每一项的字符串长度进行排序

let arr = ['132','qwedd','q','2r','qwd'];
arr.sort(function(a,b){
    return a.length>b.length;
})
//输出 ["q", "2r", "132", "qwd", "qwedd"]
const arr = ['132','qwedd','q','2r','qwd']
const rtn = arr.map(i => ({raw: i, len: i.length}))
                .sort((p, n) => n.len - p.len)
                .map(i => i.raw)
console.log(rtn) // [ 'qwedd', '132', 'qwd', '2r', 'q' ]

 

你可能感兴趣的:(js,js数组排序,数组字符串排序)