This question already has an answer here:
这个问题在这里已有答案:
- Sort array by firstname (alphabetically) in Javascript 15 answers
- 在Javascript 15答案中按名字(按字母顺序)对数组进行排序
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..
我有一个对象数组,每个对象都有一个属性名称,一个字符串。我想通过这个属性对数组进行排序。我希望他们按以下方式排序..
`ABC`
`abc`
`BAC`
`bac`
etc...
How would I achieve this in JavaScript?
我如何在JavaScript中实现这一目标?
5 个解决方案
#1
43
There are 2 basic ways:
有两种基本方法:
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 a
and à
will be sorted as the same letter.
请注意,第二个版本会忽略变音符号,因此a和à将被排序为相同的字母。
Now the problem with both these ways is that they will not sort uppercase ABC
before 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 localeCompare
instead 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
#3
1
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/
看到那里http://jsfiddle.net/p8Gny/1/
#4
1
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);
});
#5
1
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).
})
#1
43
There are 2 basic ways:
有两种基本方法:
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 a
and à
will be sorted as the same letter.
请注意,第二个版本会忽略变音符号,因此a和à将被排序为相同的字母。
Now the problem with both these ways is that they will not sort uppercase ABC
before 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 localeCompare
instead 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
#2
#3
1
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/
看到那里http://jsfiddle.net/p8Gny/1/
#4
1
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);
});
#5
1
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).
})