欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

json数组按照日期先后排序

程序员文章站 2024-01-22 16:12:40
var allMyApp = [ {"startDate": "2018-07-07 12:30:00",'name':'aa'}, {"startDate": "2018-07-09 12:30:00",'name':'bb'}, {"startDate": "2018-07-05 12:30:0... ......
var allMyApp = [
                {"startDate": "2018-07-07 12:30:00",'name':'aa'},
                {"startDate": "2018-07-09 12:30:00",'name':'bb'},
                {"startDate": "2018-07-05 12:30:00",'name':'cc'},
                {"startDate": "2018-07-01 12:30:00",'name':'dd'}
            ];
            var newArr = [];
            for(var p = 0; p < allMyApp.length; p++){
                  newStartDate = (allMyApp[p].startDate).replace(new RegExp("-", "gm"), "/");
                startHaoMiao = (new Date(newStartDate)).getTime(); //得到毫秒数
                newArr.push(allMyApp[p])
                newArr[p].timeStartApp = startHaoMiao;
            }
            console.log(newArr);
            var arr = newArr.sort(sortDate);
        //arr就是最终的排序后的数组 console.log(arr); function sortDate(a,b){ return a.timeStartApp-b.timeStartApp }