function sortting(obj) {
let result = []
var count = 0; //用来创建新数组
var max = 0; //找出系数最大值
for (var item in obj) {
var temp = obj[item].substring(obj[item].length - 1, obj[item].length);
if (result[count] != undefined) {
var curentcount = count;
if (result[count].name == temp) {
result[count].value = +Number(obj[item].substring(0, obj[item].length - 1))
} else {
var book = false
while (count--) {
if (result[count].name == temp) {
result[count].value += Number(obj[item].substring(0, obj[item].length - 1))
book = true
}
if (result[count].value > max) {
max = result[count].value
}
}
count = curentcount;
if (book == false) {
count++;
result[count] = {
name: obj[item].substring(obj[item].length - 1, obj[item].length),
value: Number(obj[item].substring(0, obj[item].length - 1))
};
if (result[count].value > max) {
max = result[count].value
}
}
}
} else {
result[count] = {
name: obj[item].substring(obj[item].length - 1, obj[item].length),
value: Number(obj[item].substring(0, obj[item].length - 1))
};
if (result[count].value > max) {
max = result[count].value
}
}
}
console.log(result,max)
}
let result = ['1a', '3b', '1c', '2a', '7b', '2a','6d','10a','20b']
sortting(result)
第一种方法估计就是另开数组把系数作为value,字母作为key,在另外操作,这样不是最优解,虽然我不知道我的是不是,但是效率是n,应该会好点