如何使用jquery或javascript对对象数组进行排序 [英] How to sort an array of objects with jquery or javascript
本文介绍了如何使用jquery或javascript对对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象数组:
var array = [(id, name, value),(id, name, value)]; //and so on
如何使数组按属性名称的升序排序(array [i] [1])
?
How do I get the array to be sorted in ascending order of the atribute name (array[i][1])
?
我试过这样做: array [i] [1] .sort()
,但这不起作用。
I've tried to do this: array[i][1].sort()
, but that doesn't work.
请帮助我!
编辑:数组可以包含两个以上的对象!它可以包含数百个。
the array can contain more than two objects! It can contain hundreds.
编辑:
为什么这个问题被标记为重复,当它被问到2年前重复问题?
Why is this question marked as a duplicate, when it was asked 2 years before the "duplicated" question?
推荐答案
//This will sort your array
function SortByName(a, b){
var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
array.sort(SortByName);
这篇关于如何使用jquery或javascript对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文