javascript sorting array based on another array

后端 未结 1 570
故里飘歌
故里飘歌 2020-11-30 09:17

I have a global array

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

and my array

var my =         


        
相关标签:
1条回答
  • 2020-11-30 10:00

    One possible way:

    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 );

    0 讨论(0)
提交回复
热议问题