我根据一些值构建了一个 K/V 对数组。
这是数据结构:
var selItemsDimArray = []
selItemsDimArray.push({
'examinedElem': multiselected[i],
'x': bb.x,
'y': bb.y,
'x2': (bb.x + bb.width),
'y2': (bb.y + bb.height),
'height': bb.height,
'width': bb.width
});
How can I sort selItemsDimArray numerically(lowest to highest) based on element.x property?
“深受喜爱”的 W3schools 给了我一个例子:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b}); //Where did a and b come from?
最佳答案
就像这样:
selItemsDimArray.sort(function(a, b) {
// here a and b are two items from selItemsDimArray array
// which means it is possible access and compare the x property for both items
return a.x - b.x;
});
关于javascript - 根据属性值降序对键值对进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698957/