json数组按照日期先后排序

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 }

 

posted @ 2018-07-09 16:02  青青子衿619  阅读(2708)  评论(0编辑  收藏  举报