1

Hi I want to sort an array of point objects in javascript so that the array,

[{x: 220, y: 1080}, {x: 1, y: 0}, {x: 0, y: 1080}]

becomes

[{x: 0, y: 1080}, {x: 1, y: 0}, {x: 220, y: 1080}]

Thanks in advance

4

2 回答 2

6
arr.sort(function(a,b) {
    if( a.x == b.x) return a.y-b.y;
    return a.x-b.x;
});

这是sort...的基本功能

于 2012-04-20T13:44:37.320 回答
2
var sorted = unsorted.sort(function(a,b) { return a.x - b.x })
于 2012-04-20T13:44:10.697 回答