嗨,我有一系列对象

cards = [
{ Asset: "2C.jpg",
  CardName: "2C",
  CardPlayed: 0,
  Playbell: 0,
  PlayerName: "player1",
  Rank: 2,
  Suit: "C"
},
{ Asset: "9S.jpg",
  CardName: "9S",
  CardPlayed: 0,
  Playbell: 0,
  PlayerName: "player2",
  Rank: 9,
  Suit: "S"
},
{ Asset: "6D.jpg",
  CardName: "6D",
  CardPlayed: 0,
  Playbell: 0,
  PlayerName: "player1",
  Rank: 6,
  Suit: "D"
}];


并且我需要基于Suit属性对那些对象进行排序,但仅针对具有PlayerName属性值等于"player1"的对象,并在此先感谢您的帮助。

最佳答案

要先按PlayerName然后按Suit的顺序对数组进行排序:

cards.sort(function(x, y){
  return (
    x.PlayerName < y.PlayerName ? -1 :
    x.PlayerName > y.PlayerName ? 1 :
    x.Suit < y.Suit ? -1 :
    x.Suit > y.Suit ? 1 :
    0
  );
});

09-25 12:02