如何排序对象数组?

时间:2012-08-04 19:46:24

标签: javascript

  

可能重复:
  How to sort an array of javascript objects?

我有一系列对象:

var arr = []
arr[0] = new Object()
...
arr[n] = new Object()

我想通过arr [i] .getSortOrder()获取排序数组,例如,其中arr [i] .getSortOrder()返回整数值。怎么做?

1 个答案:

答案 0 :(得分:2)

仅供记录:

arr.sort(function(a, b){return a.getSortOrder() - b.getSortOrder();});

有关详细信息,请参阅Sorting an array of JavaScript objectsMDN docs for the sort() method