问题
I have a json file like
var data = {
"list":[
{
"g": "zas",
"e": "wef"
},
{
"g": "abc",
"e": "ew"
},
{
"g": "wee",
"e": "asd"
},.....
How do I sort the entries w.r.t "g" so that I get abc then wee and then zas.... I want the sort changes permanently in the json file
回答1:
In JavaScript, you can use a custom comparator function to sort an array like so:
data.list.sort(function (a, b)
{
return a.g > b.g ? 1 : a.g < b.g ? -1 : 0
});
Array.sort @ MDC
来源:https://stackoverflow.com/questions/6238830/sort-a-json-file-with-2-entries