Possible Duplicate:
Sorting objects in an array by a field value in JavaScript可能重复:使用JavaScript中的字段值对数组中的对象进行排序
How can I sort an array of objects numerically (by id) then alphabetically (by name)?
如何按字母顺序(按名称)按字母顺序(按名称)对对象数组进行排序?
The current way is providing invalid output.
目前的方式是提供无效输出。
This is the object i'm trying to sort through
这是我试图整理的对象
var items = [
{
"id": 165,
"name": "a"
},
{
"id": 236,
"name": "c"
},
{
"id": 376,
"name": "b"
},
{
"id": 253,
"name": "f"
},
{
"id": 235,
"name": "e"
},
{
"id": 24,
"name": "d"
},
{
"id": 26,
"name": "d"
}
]
and the way i'm trying to sort
以及我试图排序的方式
items.sort(function(a, b) {
return (a.id - b.id);
}).sort(function(a, b) {
return (a.name - b.name);
});
here is the jsfiddle.
这是jsfiddle。
http://jsfiddle.net/jh4xb/
EDIT: Sorry for the confusion, I've been so confused by this problem for a while.
编辑:对不起,我已经对这个问题感到困惑了一段时间。
What I'm trying to accomplish is to sort by the highest id first, and then sort alphabetically so in the end it should look like:
我想要完成的是先按最高ID排序,然后按字母顺序排序,最后看起来像:
var items = [
{
"id": 376,
"name": "b"
},
{
"id": 253,
"name": "f"
},
{
"id": 236,
"name": "c"
},
{
"id": 235,
"name": "e"
},
{
"id": 165,
"name": "a"
},
{
"id": 26,
"name": "d"
},
{
"id": 24,
"name": "d"
}
]
5 个解决方案
#1
38
I think it's better done just with...
我觉得最好只用......
items.sort(function(a, b) {
return a.id - b.id || a.name.localeCompare(b.name);
});
The second sort will basically negate the first one, so you have to do it once. )
第二种基本上会否定第一种,所以你必须做一次。 )
a.id - b.id || a.name.localeCompare(b.name)
expression will first compare the id
s; only if they are equal, it will compare the names (and return the result of this comparison).
a.id - b.id || a.name.localeCompare(b.name)表达式将首先比较id;只有它们相等,它才会比较名称(并返回此比较的结果)。
If you need to reverse the ordering, swap the positions (b.id - a.id
, etc.) - or just negate the whole thing:
如果你需要反转顺序,交换位置(b.id - a.id等) - 或者只是否定整个事情:
items.sort(function(a, b) {
return - ( a.id - b.id || a.name.localeCompare(b.name) );
});
Here's the JSFiddle (have to simplify the data a bit to show my point).
这是JSFiddle(必须简化数据以显示我的观点)。
#2
5
IF you mean that you wanted them sorted by id
and if the id
matches, you want them sorted by name
then use this:
如果你的意思是你希望它们按id排序,如果id匹配,你希望它们按名称排序,然后使用:
items.sort(function(a, b) {
if (a.id !== b.id) {
return a.id - b.id
}
if (a.name === b.name) {
return 0;
}
return a.name > b.name ? 1 : -1;
});
Otherwise your question is unclear.
否则你的问题不清楚。
#3
1
Create a general function to sort it anyway you want. Check the dynamicSort function below
无论如何,创建一个通用函数来对其进行排序。检查下面的dynamicSort函数
var items = [
{
"id": 165,
"name": "a"},
{
"id": 236,
"name": "b"},
{
"id": 376,
"name": "c"},
{
"id": 253,
"name": "d"},
{
"id": 235,
"name": "e"},
{
"id": 24,
"name": "f"}
];
function dynamicSort(property) {
return function(a, b) {
return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
}
}
items.sort(dynamicSort('name')).sort(dynamicSort('id'));
console.log(items);
items.sort(dynamicSort('id')).sort(dynamicSort('name'));
console.log(items);
#4
0
I'm not seeing any errors in Chrome, but it's not sorting correctly.
我没有在Chrome中看到任何错误,但它没有正确排序。
If I change your sort code to this:
如果我将您的排序代码更改为:
var firstRun = items.sort(function(a, b) {
return (a.id - b.id);
});
var secondRun = firstRun.sort(function(a, b) {
return (a.name - b.name);
});
console.log(secondRun);
I believe it's sorting correctly. Your example doesn't really need to do the second sort as each number (id) is different. But when I make b, d and e all have an ID of 235, it sorts fine.
我相信它正确排序。您的示例实际上不需要进行第二次排序,因为每个数字(id)都不同。但是当我制作b,d和e都有一个235的ID,它排序很好。
#5
0
your problem is how you're sorting the letters.a.name - b.name
你的问题是如何排序字母。 a.name - b.name
isn't doing what you think. It's not evaluating them in lexigraphical order. replace it with
没有做你的想法。它不是以lexigraphical顺序评估它们。用它替换它
a.name < b.name ? -1 : 1;
a.name
#1
38
I think it's better done just with...
我觉得最好只用......
items.sort(function(a, b) {
return a.id - b.id || a.name.localeCompare(b.name);
});
The second sort will basically negate the first one, so you have to do it once. )
第二种基本上会否定第一种,所以你必须做一次。 )
a.id - b.id || a.name.localeCompare(b.name)
expression will first compare the id
s; only if they are equal, it will compare the names (and return the result of this comparison).
a.id - b.id || a.name.localeCompare(b.name)表达式将首先比较id;只有它们相等,它才会比较名称(并返回此比较的结果)。
If you need to reverse the ordering, swap the positions (b.id - a.id
, etc.) - or just negate the whole thing:
如果你需要反转顺序,交换位置(b.id - a.id等) - 或者只是否定整个事情:
items.sort(function(a, b) {
return - ( a.id - b.id || a.name.localeCompare(b.name) );
});
Here's the JSFiddle (have to simplify the data a bit to show my point).
这是JSFiddle(必须简化数据以显示我的观点)。
#2
5
IF you mean that you wanted them sorted by id
and if the id
matches, you want them sorted by name
then use this:
如果你的意思是你希望它们按id排序,如果id匹配,你希望它们按名称排序,然后使用:
items.sort(function(a, b) {
if (a.id !== b.id) {
return a.id - b.id
}
if (a.name === b.name) {
return 0;
}
return a.name > b.name ? 1 : -1;
});
Otherwise your question is unclear.
否则你的问题不清楚。
#3
1
Create a general function to sort it anyway you want. Check the dynamicSort function below
无论如何,创建一个通用函数来对其进行排序。检查下面的dynamicSort函数
var items = [
{
"id": 165,
"name": "a"},
{
"id": 236,
"name": "b"},
{
"id": 376,
"name": "c"},
{
"id": 253,
"name": "d"},
{
"id": 235,
"name": "e"},
{
"id": 24,
"name": "f"}
];
function dynamicSort(property) {
return function(a, b) {
return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
}
}
items.sort(dynamicSort('name')).sort(dynamicSort('id'));
console.log(items);
items.sort(dynamicSort('id')).sort(dynamicSort('name'));
console.log(items);
#4
0
I'm not seeing any errors in Chrome, but it's not sorting correctly.
我没有在Chrome中看到任何错误,但它没有正确排序。
If I change your sort code to this:
如果我将您的排序代码更改为:
var firstRun = items.sort(function(a, b) {
return (a.id - b.id);
});
var secondRun = firstRun.sort(function(a, b) {
return (a.name - b.name);
});
console.log(secondRun);
I believe it's sorting correctly. Your example doesn't really need to do the second sort as each number (id) is different. But when I make b, d and e all have an ID of 235, it sorts fine.
我相信它正确排序。您的示例实际上不需要进行第二次排序,因为每个数字(id)都不同。但是当我制作b,d和e都有一个235的ID,它排序很好。
#5
0
your problem is how you're sorting the letters.a.name - b.name
你的问题是如何排序字母。 a.name - b.name
isn't doing what you think. It's not evaluating them in lexigraphical order. replace it with
没有做你的想法。它不是以lexigraphical顺序评估它们。用它替换它
a.name < b.name ? -1 : 1;
a.name