gpt4 book ai didi

javascript - 使用下划线 JS 降序排序

转载 作者:行者123 更新时间:2023-12-01 02:39:54 25 4
gpt4 key购买 nike

我有下面的代码,我需要根据 (y) 的值按下划线 JS 进行排序。

请在下面找到我的代码。我尝试先上升,然后反向。不幸的是,它根本没有为我进行排序,并且显示数据而不进行排序。

        function getMessageNames(){
var rowIter = 1;
//document.getElementById("demo2").innerHTML += "from getMessageNames="+calltypes;
for (var i in calltypes){
keyId = calltypes[i].label;
for (var index in keyMsgNames){
if(keyMsgNames[index].Id == keyId){
var row = {"label":keyMsgNames[index].Name,"y":calltypes[i].y};
messageNames.push(row);
break;
}
}


// Result:
//document.getElementById("demo2").innerHTML += "descending="+descending;
}

var ascending = _.sortBy(messageNames, ['y']);
// Then get DESC
var descending = ascending.reverse();
for (var index in descending){
addRow("CallTable", [ descending[index].label, descending[index].y], rowIter++);
}
createChart(descending,"Distribution on Key Messages",chartContainer);
}

function createChart(data,title,container){
var chart = new CanvasJS.Chart(container, {
animationEnabled: true,
title: {
text: title
},
data: [{
type: "pie",
startAngle: 240,
yValueFormatString: "",
indexLabel: "{label} '('{y}')'",
dataPoints: data
}]
});
chart.render();
}

function addRow(tblName, colData, rowNum){
var table = document.getElementById(tblName);
var tableRow = table.insertRow(rowNum);

for (var index in colData){
var cell = tableRow.insertCell(index);
cell.innerHTML = colData[index];
}
}

最佳答案

sortBy需要一个属性数组

替换

var ascending = _.sortBy(messageNames, 'y'); 

var ascending = _.sortBy(messageNames, ['y']); 

关于javascript - 使用下划线 JS 降序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47646268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com