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}]
bees
[{x: 0, y: 1080}, {x: 1, y: 0}, {x: 220, y: 1080}]
Thanks in advance
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}]
bees
[{x: 0, y: 1080}, {x: 1, y: 0}, {x: 220, y: 1080}]
Thanks in advance
Share Improve this question edited Apr 20, 2012 at 15:26 sgowd 2,26222 silver badges30 bronze badges asked Apr 20, 2012 at 13:42 user1346697user1346697 111 silver badge2 bronze badges 1-
1
points.sort(function(a,b) { return a.x-b.x || a.y-b.y })
– georg Commented Apr 20, 2012 at 13:54
2 Answers
Reset to default 7arr.sort(function(a,b) {
if( a.x == b.x) return a.y-b.y;
return a.x-b.x;
});
This is basic functionality of sort
...
var sorted = unsorted.sort(function(a,b) { return a.x - b.x })