去后台数据,根据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))

返回 结果是:

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

(例如以下:先根据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); //根据价格排序

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值