我有以下代码,我需要根据(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];
}
}
答案 0 :(得分:2)
sortBy需要一系列属性
替换
var ascending = _.sortBy(messageNames, 'y');
与
var ascending = _.sortBy(messageNames, ['y']);