10

我有一个全局数组

var g = [ "jack", "queen", "king", "10", "ace","7", "8", "9"];

和我的阵列

var my = ["9","king","7","ace"];

该数组将根据全局数组进行排序g

如果我对数组进行排序,输出将是

["king","ace","7","9"]

我玩了很多数组,但无法做到这一点。请帮我解决这个问题。提前致谢。

4

1 回答 1

34

一种可能的方式:

var g = ['jack', 'queen', 'king', '10', 'ace', '7', '8', '9'];
var my = ['9', 'king', '7', 'ace'];

my.sort(function(a, b) {
  return g.indexOf(a) - g.indexOf(b);
});

console.log( my );

于 2013-06-06T10:30:57.657 回答