javaScript 数组多条件排序

let player1 = {
    score: 10,
    state: 3
};


let player2 = {
    score: 5,
    state: 2
};

let player3 = {
    score: 6,
    state: 2
};

let player4 = {
    score: 1,
    state: 2
};

let players = [player1, player2, player3, player4];

players.sort(function (a, b) {
    if (a.state != b.state) {
        return a.state - b.state;
    }else {
        return b.score - a.score;
    }
});

console.log( JSON.stringify(players));
[{"score":6,"state":2},{"score":5,"state":2},{"score":1,"state":2},{"score":10,"state":3}]

你可能感兴趣的:(javaScript,nodejs)