Javascript custom sort algorithm according to another array

后端 未结 4 1950
余生分开走
余生分开走 2021-01-25 18:55

I have this two arrays

var refArray = [\'India\',\'Pakistan\',\'Nepal\',\'Bhutan\',\'SreeLanka\',\'Singapore\',\'Thailand\',\'China\',\'Russia\']
var beenThere          


        
4条回答
  •  故里飘歌
    2021-01-25 19:41

    Try to use indexOf:

    beenThere.sort(function(a, b) {
        return refArray.indexOf(a) - refArray.indexOf(b); 
    }); // ['India','Bhutan','Russia']
    

提交回复
热议问题