用2个条目对json文件进行排序 [英] sort a json file with 2 entries

查看:136
本文介绍了用2个条目对json文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的json文件

I have a json file like

 var data = {
     "list":[

{
    "g": "zas",
    "e": "wef"
},
{
    "g": "abc",
    "e": "ew"
},
{
    "g": "wee",
    "e": "asd"
},.....

如何对带有"g"的条目进行排序 这样我得到abc,然后是wee,然后是zas .... 我想将排序更改永久保存在json文件中

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

推荐答案

在JavaScript中,您可以使用自定义比较器函数对数组进行排序,如下所示:

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

Array.sort @ MDC

这篇关于用2个条目对json文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆