Javascript custom sort algorithm according to another array

后端 未结 4 1939
余生分开走
余生分开走 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:47

    Just compare the indices of each element in the refArray using indexOf method.

    beenThere.sort(function(a,b){
    return refArray.indexOf(a)-refArray.indexOf(b);
    })
    

提交回复
热议问题