对多个键上的对象/数组中的数据进行排序

时间:2017-06-25 04:03:36

标签: javascript

我有一个多人游戏,游戏数据存储如下:

var gameplays = [
    {id: "1", player1: "bob", player2: "tim", score1: 2, score2: 14},
    {id: "2", player1: "bob", player2: "tim", score1: 7, score2: 3},
    {id: "3", player1: "bob", player2: "tim", score1: 6, score2: 10},
    {id: "4", player1: "bob", player2: "tim", score1: 5, score2: 1}
];

通过搜索"得分1和#34;从所有游戏中找到前5个高分的最有效方法是什么?和"得分2"并输出如下:

HIGHSCORES
1. Tim - 14
2. Tim - 10
3. Bob - 7
4. Bob - 6
5. Bob - 5

4 个答案:

答案 0 :(得分:3)

var scores = [];
for (var i = 0; i < gameplays.length; i++) {
    scores.push({score: gameplays[i].score1, name: gameplays[i].player1});
    scores.push({score: gameplays[i].score2, name: gameplays[i].player2});
}

scores.sort(function (a, b) {
    return b.score - a.score;
});

scores.splice(0, 5);

首先,获得分数并将其与分数和个人名称一起展平。

然后,我们对数组进行排序,拼接将获得名称前5位。

答案 1 :(得分:1)

TextView[][] tvs;
String[][] values;

for(int i = 0 ; i < tvs.length ; i++) {
    for(int j = 0 ; j < tvs[i].length ; j++) {
        tvs[i][j].setText(values[i][j]);
    }
}

首先,将所有相关游戏信息写入一个对象数组,每个对象包含一个与玩家姓名对应的玩家密钥和一个与分数对应的分数键:

function my_class() {
    this.do_something( data ) {
          return new Promise(function(resolve,reject){
                this.get_data().then(function(dataReceiced){
                                    //reading promise retuned by    get_data();
              if(data == dataReceiced){
                  do_the_thing(); //need to promisify this also to use then();
                  resolve('Done');
              } else {   reject('Error'); }
                });  
           });
        }
    }

    this.get_data = function() {
        return library.get_info( arguments ); //can't do this, returns a promise
    }
}
i=0; // keep i global
var stuff = new my_class();

function doStuff(){
  if(i < len){ mainStuff(); }
}

function mainStuff(){
    stuff.do_something( i ).then(function(){
       i++; 
       doStuff();
    });

};
  doStuff(); // to start process. 

然后,按照分数的降序对数组进行排序,然后只保留前5个切片。

const gameplays = [
    {id: "1", player1: "bob", player2: "tim", score1: 2, score2: 14},
    {id: "2", player1: "bob", player2: "tim", score1: 7, score2: 3},
    {id: "3", player1: "bob", player2: "tim", score1: 6, score2: 10},
    {id: "4", player1: "bob", player2: "tim", score1: 5, score2: 1}
];

最后,显示前5个分数。

const results = [];

gameplays.forEach(game => {
  for(let i = 1; i <= 2; i++) {
    results.push({});
    results[results.length - 1].player = `${game[`player${i}`].slice(0, 1).toUpperCase()}${game[`player${i}`].slice(1).toLowerCase()}`;
    results[results.length - 1].score = game[`score${i}`];
  }
});

答案 2 :(得分:0)

我认为你可以很容易地做到这一点:

gameplays.sort(function(_a, _b){
    var a = _a.score1 > _a.score2 ? _a.score1 : _a.score2;
    var b = _b.score1 > _b.score2 ? _b.score1 : _b.score2;

    if(a < b) {
      return 1;
    }

    if(a > b) {
      return -1;
    }

    return 0;
})

然后,您可以通过以下方式访问前五名:

gameplays.slice(0, 5)

答案 3 :(得分:0)

如果您要进行基准测试,我会感兴趣的是&#34;功能性&#34;与Ramda一起解决方案。

var gameplays = [
    {id: "1", player1: "bob", player2: "tim", score1: 2, score2: 14},
    {id: "2", player1: "bob", player2: "tim", score1: 7, score2: 3},
    {id: "3", player1: "bob", player2: "tim", score1: 6, score2: 10},
    {id: "4", player1: "bob", player2: "tim", score1: 5, score2: 1}
];

// find the 5 top highscores regardless which player
const normalizeScore = ({id, player1, score1, player2, score2}) => 
    [{id, player1, score: score1}, {id, player2, score: score2}];
const sortByScore = (play1, play2) => play2.score - play1.score;

const normalizeGameplays = R.chain(normalizeScore); // chain === flatMap
const sortGameplays = R.sort(sortByScore);
const topFive = R.slice(0, 5);

const highscore = R.compose(topFive, sortGameplays, normalizeGameplays);

console.log(highscore(gameplays));

@See:https://jsbin.com/wixowu/edit?html,js,console