jQuery 按字母顺序对 JSON 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8419103/
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 alphabetically
提问by ayyp
I have a JSON object generated based on data stored in a table. I then need to be able to sort it in different ways, but when I do JSON.stringify(array)
and try to sort from there it doesn't work. When I try to just do array.sort();
it will change the order, but ultimately doesn't work. I don't have much experience with JSON and how to operate it so I'm not sure what else to try. After sorting it I need to go through and rewrite the table with the selected category in alphabetical order.
我有一个基于存储在表中的数据生成的 JSON 对象。然后我需要能够以不同的方式对其进行排序,但是当我这样做JSON.stringify(array)
并尝试从那里进行排序时它不起作用。当我尝试这样做时,array.sort();
它会改变顺序,但最终不起作用。我对 JSON 以及如何操作它没有太多经验,所以我不确定还能尝试什么。排序后,我需要按照字母顺序使用所选类别重写表格。
The JSON looks like this:
JSON 如下所示:
var arr = [{
"Functional Category":"T-Shirt",
"Brand Name":"threadless",
"When Obtained":"Last 3 Months",
"How Obtained":"Purchased",
"How Often Worn":"Monthly",
"Where It's Made":"India",
"Has a Graphic":"Yes"}]
I have a fiddle setup here: http://jsfiddle.net/Skooljester/88HVZ/1/and I have tried what was suggested herebut was unable to make it work.
我在这里有一个小提琴设置:http: //jsfiddle.net/Skooljester/88HVZ/1/我已经尝试了这里的建议,但无法使其工作。
I have two questions, one: how do I go about accomplishing this, and two: is there a better way to do the sorting?
我有两个问题,一:我如何去完成这个,二:有没有更好的方法来进行排序?
采纳答案by Bart
First, define a compare function:
首先,定义一个比较函数:
function compare(el1, el2, index) {
return el1[index] == el2[index] ? 0 : (el1[index] < el2[index] ? -1 : 1);
}
Then, to sort the array on the first column, use this:
然后,要对第一列的数组进行排序,请使用以下命令:
array.sort(function(el1,el2){
return compare(el1, el2, "Functional Category")
});
Or, to sort on the first column A-Z, and on the second column Z-A if the first column is equal:
或者,在第一列 AZ 上排序,如果第一列相等,则在第二列 ZA 上排序:
array.sort(function(el1,el2){
var compared = compare(el1, el2, "Functional Category")
return compared == 0 ? -compare(el1, el2, "Brand Name") : compared;
});
回答by MANISHDAN LANGA
see below code ....
见下面的代码....
function sortObject(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}
Does that help?
这有帮助吗?
回答by Roy Tinker
The Mozilla developer documentation has a helpful explanation of the sort function. You can provide a function as a parameter that tells the browser how to do the sort.
Mozilla 开发人员文档对 sort 函数进行了有用的解释。您可以提供一个函数作为参数,告诉浏览器如何进行排序。
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
Some pseudo-code from that link, with the myArray.sort call added and the function name removed:
来自该链接的一些伪代码,添加了 myArray.sort 调用并删除了函数名称:
myArray.sort(function(a, b) {
if (a is less than b by some ordering criterion)
return -1;
if (a is greater than b by the ordering criterion)
return 1;
// a must be equal to b
return 0;
});
EDIT: It looks like you have a single-element array with an object as the element. Objects are unordered. You have to transform it to an array before it can be sorted. Try this (with the Object.keys and Array.prototype.map functions added via augment.jsfor older browsers):
编辑:看起来您有一个以对象为元素的单元素数组。对象是无序的。您必须先将其转换为数组,然后才能对其进行排序。试试这个(使用 Object.keys 和 Array.prototype.map 函数为旧浏览器添加通过增加.js):
var result = Object.keys(myArray[0]).sort().map(function(key) {
return [key,myArray[0][key]];
});
This will give you a sorted, nested array of the form:
这将为您提供以下形式的排序嵌套数组:
[["Brand Name","threadless"],
["Function Category","T-Shirt"],
...
]
回答by Sudhir Bastakoti
Something of this kind might be on help:
这种类型的东西可能有帮助:
function (obj) { var a = [],x; for(x in obj){ if(obj.hasOwnProperty(x)){ a.push([x,obj[x]]); } } a.sort(function(a,b){ return a[0]>b[0]?1:-1; }) return a; }
回答by Betard Fooser
If your intent is to allow the user to sort the table dynamically, then my approach may be slightly different.
如果您的意图是允许用户对表格进行动态排序,那么我的方法可能略有不同。
There are plenty of jQuery table sorting plugins. I have had good success with this one: http://tablesorter.com/docs/
有很多 jQuery 表格排序插件。我在这个方面取得了很好的成功:http: //tablesorter.com/docs/
Allows multi-column sort, a default sort, wire up to events, etc... etc...
允许多列排序、默认排序、连接到事件等……等等……
This way you can build your table as you already are, and sorting can be done secondary even before page output.
通过这种方式,您可以按原样构建表格,甚至可以在页面输出之前进行二次排序。