根据对象的属性之一对对象的 JavaScript 数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5421253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort JavaScript array of Objects based on one of the object's properties
提问by Skizit
I've got an array of objects, each of which has a property name, a string. I'd like to sort the array by this property. I'd like them sorted in the following way..
我有一个对象数组,每个对象都有一个属性name,一个字符串。我想按此属性对数组进行排序。我希望它们按以下方式排序..
`ABC`
`abc`
`BAC`
`bac`
etc...
How would I achieve this in JavaScript?
我将如何在 JavaScript 中实现这一点?
回答by Martin Jespersen
There are 2 basic ways:
有2种基本方式:
var arr = [{name:"ABC"},{name:"BAC"},{name:"abc"},{name:"bac"}];
arr.sort(function(a,b){
var alc = a.name.toLowerCase(), blc = b.name.toLowerCase();
return alc > blc ? 1 : alc < blc ? -1 : 0;
});
or
或者
arr.sort(function(a,b){
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});
Be aware that the 2nd version ignore diacritics, so aand àwill be sorted as the same letter.
请注意,第二个版本忽略变音符号,因此a和à将按相同的字母排序。
Now the problem with both these ways is that they will not sort uppercase ABCbefore lowercase abc, since it will treat them as the same.
现在这两种方式的问题是它们不会ABC在小写之前对大写进行排序abc,因为它会将它们视为相同的。
To fix that, you will have to do it like this:
要解决这个问题,你必须这样做:
arr.sort(function(a,b){
var alc = a.name.toLowerCase(), blc = b.name.toLowerCase();
return alc > blc ? 1 : alc < blc ? -1 : a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
});
Again here you could choose to use localeCompareinstead if you don't want diacritics to affect the sorting like this:
localeCompare如果您不希望变音符号像这样影响排序,您可以再次选择使用:
arr.sort(function(a,b){
var lccomp = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
return lccomp ? lccomp : a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
});
You can read more about sort here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
您可以在此处阅读有关排序的更多信息:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
回答by JRL
You can pass-in a sort function reference to Array.sort.
您可以传入对 Array.sort的排序函数引用。
回答by Tim Down
You can pass a custom sorting function to the sort()method of an array. The following will do the trick and take your requirements about capitalization into account.
您可以将自定义排序函数传递给sort()数组的方法。以下将解决问题并考虑您对大写的要求。
objects.sort(function(o1, o2) {
var n1 = o1.name, n2 = o2.name, ni1 = n1.toLowerCase(), ni2 = n2.toLowerCase();
return ni1 === ni2 ? (n1 === n2 ? 0 : n1 > n2 ? 1 : -1) : (ni1 > ni2 ? 1 : -1);
});
回答by Aleadam
Slightly modified from Sorting an array of objects,
从排序对象数组中稍微修改,
yourobject.sort(function(a, b) {
var nameA = a.name, nameB = b.name
if (nameA < nameB) //Sort string ascending.
return -1
if (nameA > nameB)
return 1
return 0 //Default return value (no sorting).
})
回答by avrelian
objects.sort(function(c, d) {
return (
c['name'].toLowerCase() > d['name'].toLowerCase() ||
c['name'] > d['name']
) ? 1 : -1;
});
see there http://jsfiddle.net/p8Gny/1/

