“9999 22 1234000 11 11 123 10003 44444444 2000”
- 跟举字符串的每个值的权重正序排序
权重: 每个字符串相加的值
例如: 9999 权重是36,1234000权重是10,就是每个字符串自身的值相加 - 复杂+
当两个字符串权重相同的时候,按照他们数值大小排序 例如: 2000 10003 22
function orderWeight(str) {
let sum = (str) => str.split("").reduce((a, b) => a + +b, 0);
let comp = (a, b) =>
sum(a) == sum(b) ? a.localeCompare(b) : sum(a) - sum(b);
return str.split(" ").sort(comp).join(" ");
}
orderWeight("9999 22 1234000 11 11 123 10003 44444444 2000"); // '11 11 2000 10003 22 123 1234000 44444444 9999'
console.log(orderWeight("9999 22 1234000 11 11 123 10003 44444444 2000"));