sort a json file with 2 entries

被刻印的时光 ゝ 提交于 2020-01-06 13:08:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!