javascript - 根据属性值降序对键值对进行排序

标签 javascript arrays sorting

我根据一些值构建了一个 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;
});

Array.prototype.sort on MDN

关于javascript - 根据属性值降序对键值对进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698957/

相关文章:

javascript - 验证函数后返回 'true'

python - 根据其他数组的阈值创建新的 2d numpy 数组

arrays - 根据用户在 Swift 中的选择从数组加载自定义单元格值

php - usort 对多个字段进行排序

cocoa - iTunes 中具有额外排序功能的新的较高表格标题行是如何实现的?

javascript - JS/jQuery/CSS --- 用其中任何一个改变元素的水平顺序?

javascript - 我的 {{url}} 怎么了?

c - C 语言第 2 级数组的理想数据类型

ios - 1 个按钮有 4 个操作,我可以按特定顺序对操作进行排序吗?

javascript - 仅在特定屏幕上渲染子组件中的按钮