去后台数据,根据id重新排序

var Info =  [{
        "bMatchId": "3771",
        "MatchDate": "2018-07-05 17:00:00",
    }, {
        "bMatchId": "3772",
        "MatchDate": "2018-07-05 18:00:00",
    }, {
        "bMatchId": "3770",
        "MatchDate": "2018-07-05 16:00:00",
    }]

 

//根据bMatchId 把以上数据排列成 16:00:00---18:00:00----17:00:00这样的顺序

 data.sort(function(a, b) {
        return Date.parse(a.MatchDate.replace(/\-/g, '/')) - Date.parse(b.MatchDate.replace(/\-/g, '/'));
    });

console.log(data)//此时是按照时间新的排序

 

function getnewarr(arr) {

    var newarr = []
    var objarr = []
    arr.forEach(val => {
        newarr.push(val.bMatchId)
    })
    newarr.sort()
    newarr.forEach(id => {
        arr.forEach(item => {
            if (id === item.bMatchId) {
                objarr.push(item)
            }
        })
    })
    return objarr
}

 

//一个数组对象,根据时间搓(或者任何数字类型的),比较大小,进行重新排序

var arr=[
    {name:"小明",id:1222},
    {name:"小红",id:13},
    {name:"小花",id:112}
]

function sortRule(a,b) {
    return a.id - b.id;
}
 console.log(arr.sort(sortRule))

返回 结果是:

去后台数据,根据id重新排序_第1张图片

根据数组对象的两个属性进行排序

(例如以下:先根据heroId排序,再根据price排序)

function sortPrice(a, b) {
    if (a["price"] === b["price"]) {
        return a["heroId"] > b["heroId"] ? 1 : a["heroId"] < b["heroId"] ? -1 : 0;

    } else {

        return a["price"] > b["price"] ? 1 : -1;

    }
};


  data.sort(sortPrice); //根据价格排序

 

你可能感兴趣的:(js)