最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Sorting array of point objects in Javascript - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 7
arr.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 })
发布评论

评论列表(0)

  1. 暂无评论