javascript - 怎么样根据 JSON 中的某个字段对其进行排序?

查看:89
本文介绍了javascript - 怎么样根据 JSON 中的某个字段对其进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

  1. 我有一个 json ,具体格式如下:

{
    "text": {
        "name": "待确定",
        "desc": "22146",
        "title": "1"
    },
    "children": [{
        "text": {
            "name": "王小和",
            "desc": "12037",
            "title": "0|王小和|78|192|无业"
        },
        "children": [{
            "text": {
                "name": "陈合松",
                "desc": "5944",
                "title": "0|陈合松|50|192|粮农"
            },
            "children": [],
            "image": "/Images/40-60.png"
        }],
        "image": "/Images/60.png"
    }, {
        "text": {
            "name": "王用兵",
            "desc": "12071",
            "title": "0|王用兵|72|192|粮农"
        },
        "children": [{
            "text": {
                "name": "王新艳",
                "desc": "6247",
                "title": "0|王新艳|39|192|粮农"
            },
            "children": [{
                "text": {
                    "name": "王浩洋",
                    "desc": "20955",
                    "title": "0|王浩洋|8|192|/"
                },
                "children": [],
                "image": "/Images/3-9.png"
            }],
            "image": "/Images/30-40.png"
        }],
        "image": "/Images/60.png"
    }, {
        "text": {
            "name": "王和木",
            "desc": "12076",
            "title": "0|王和木|76|192|无业"
        },
        "children": [{
            "text": {
                "name": "王宇松",
                "desc": "6304",
                "title": "0|王宇松|47|192|粮农"
            },
            "children": [{
                "text": {
                    "name": "王权",
                    "desc": "20370",
                    "title": "0|王权|16|192|/"
                },
                "children": [],
                "image": "/Images/9-18.png"
            }],
            "image": "/Images/40-60.png"
        }],
        "image": "/Images/60.png"
    }, {
        "text": {
            "name": "王水红",
            "desc": "12096",
            "title": "0|王水红|77|192|无业"
        },
        "children": [],
        "image": "/Images/60.png"
    }, {
        "text": {
            "name": "王吉恒",
            "desc": "11868",
            "title": "0|王吉恒|73|192|粮农"
        },
        "children": [{
            "text": {
                "name": "王小兵",
                "desc": "3966",
                "title": "0|王小兵|44|192|粮农"
            },
            "children": [],
            "image": "/Images/40-60.png"
        }],
        "image": "/Images/60.png"
    }, {
        "text": {
            "name": "王和龙",
            "desc": "15862",
            "title": "0|王和龙|66|192|粮农"
        },
        "children": [],
        "image": "/Images/60.png"
    }],
    "image": "/Images/userpic_default.png"
}

每个 children 里是一个数组,我需要排序的就是这个数组里面的元素,每个元素中又有 children,所有的 children 都要进行排序。其实就是一颗树,然后排序的话是依据 text 中的 title 来进行降序排。

"0|陈合松|50|192|粮农" 具体是第二个管道符后面的一个数字(代表这个人的年龄)。

解决方案

如果你的意思是,内部排序与外部排序无关的话。
那一样是用数组sort方法进行排序。再然后,逐层递归,或者每一个'childre'遍历排序就可以了。

  var arr =   [{
    "text": {
        "title": "1"
    },
    "children": [{
        "text": {
            "title": "0"
        },
        "children": [{
            "text": {
                "title": "1"
            }
        }]
    },{
        "text": {
            "title": "4"
        },
        "children": [{
            "text": {
                "title": "1"
            },
            "children": [{
                    "text": {
                        "title": "3"
                    },
                    "children": [{
                        "text": {
                            "title": "1"
                        }
                    }, {
                        "text": {
                            "title": "0"
                        }
                    }]
                },{
                    "text": {
                        "title": "2"
                    }
                }
            ]
        }]
    },{
        "text": {
            "title": "3"
        },
        "children": [{
            "text": {
                "title": "1"
            }
        }]
    }]
},{
        "text": {
          "title": "0"
        },
        "children": []
    }];

function sortFn(a, b){
    if(a["text"]["title"] < b["text"]["title"]){
        return 1;
    }else if(a["text"]["title"] < b["text"]["title"]){
        return -1;
    }else{
        return 0;
    }
}
function fn1(item){
    if(item["children"]){
        item["children"].sort(sortFn);
        arrFn(item["children"]);
    }else{
        return 0;
    }
}
function arrFn(arr){
    arr.forEach(function(item){
        fn1(item);
    });
}

arrFn(arr);
arr.sort(sortFn);
console.log(arr);

数据做了简化处理,所以'titile'这里也偷了懒。 比较的title字段,可以自己写正则,或者如下

var str = "0|张三|49";
var age = str.split('|')[3];

获取到之后,再比较,简单加到sortFn的比较字段就行。

这篇关于javascript - 怎么样根据 JSON 中的某个字段对其进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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