javascript 使用 jquery 对 JSON 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11731501/
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 JSON with jquery
提问by Shounak Gupte
How do i sort this JSON object by first_name?
我如何按 first_name 对这个 JSON 对象进行排序?
{
1 :{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"[email protected]"},
7 :{user_id:7, first_name:"user2", last_name:"test2", email:""},
72 :{user_id:72, first_name:"testing343", last_name:"", email:""},
246:{user_id:246, first_name:"aaa", last_name:"ssss", email:""}
}
采纳答案by jAndy
First of all, your JSON stringis invalid. Clear that up first, you are forced to eval()
the string in its current form, JSON.parse()
won't work.
首先,您的JSON 字符串无效。首先清除它,您被迫以eval()
当前形式使用字符串,JSON.parse()
将无法正常工作。
Object keys are unordered by spec. You can't statically "sort" key names and freeze them in an object. But you certainly can sort the keynames and operate in that order on their values.
对象键按规范无序。您不能静态地“排序”键名并将它们冻结在一个对象中。但是您当然可以对键名进行排序并按其值的顺序进行操作。
For instance:
例如:
var json = '{1:{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"[email protected]"}, 7:{user_id:7, first_name:"user2", last_name:"test2", email:""}, 72:{user_id:72, first_name:"testing343", last_name:"", email:""}, 246:{user_id:246, first_name:"aaa", last_name:"ssss", email:""}}';
json = eval( json );
Object.keys( json ).sort(function( a,b ) {
return json[a].first_name.localeCompare( json[b].first_name );
}).forEach(function( key ) {
console.log( json[key].first_name );
});
That code assumes the availabilty of an ES5 enabled JS engine or any kind of ES5-shim library.
该代码假定启用了 ES5 的 JS 引擎或任何类型的 ES5-shim 库的可用性。
回答by darksky
Write your JSON object like the following:
像下面这样编写你的 JSON 对象:
data = [{user_id:1, first_name:"first_name1", last_name:"lastname1", email:"[email protected]"}, {user_id:7, first_name:"user2", last_name:"test2", email:""}, {user_id:72, first_name:"testing343", last_name:"", email:""}, {user_id:246, first_name:"aaa", last_name:"ssss", email:""}]
So that it's now an array. Then you can call .sort()
on it.
所以它现在是一个数组。然后就可以调用.sort()
了。
data.sort(function(a, b) {
var x = a.first_name.toLowerCase(), y = b.first_name.toLowerCase();
return x < y ? -1 : x > y ? 1 : 0;
});
回答by Matthew Blancarte
Why do you want to sort an object?
为什么要对对象进行排序?
You'll want to loop this object into an array, and then sort the array. http://jsfiddle.net/z2xsC/For example:
您需要将此对象循环到数组中,然后对数组进行排序。http://jsfiddle.net/z2xsC/例如:
//loop into array
var arr = [];
for( var key in data ){
if( data.hasOwnProperty( key ) ){
arr.push( data[key] );
}
}
//now, sort your array
arr.sort( function(a,b) {
var nameA=a.first_name.toLowerCase(),
nameB=b.first_name.toLowerCase();
if (nameA < nameB){
return -1;
} else if (nameA > nameB){
return 1
} else {
return 0
}
});