Sort array of ints based on another array of ints - Javascript Array

Javascript examples for Array:sort

Description

Sort array of ints based on another array of ints

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  w  w  w .  j ava2s  .c o  m
var SortedArray = [5, 13, 24, 12, 54, 51, 347, 4, 64];
var ArrayToSort = [34, 5, 51, 13,2,3,4];
ArrayToSort.sort(function(x, y) {
    return SortedArray.indexOf(x) - SortedArray.indexOf(y);
});
console.log(ArrayToSort);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials