Sorting array by even and odd numbers - Javascript Array

Javascript examples for Array:sort

Description

Sorting array by even and odd numbers

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() {/*w w  w . ja  v  a  2  s . co m*/
var n = [10,20,21,4,5,6,7];
n.sort(function(a,b){
 if (a % 2 != b % 2 ){
   return a%2;
  }else {
      return (a - b) > 0 ? 1 : -1;
  }
});
document.write(n.join(","))
    });

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

Related Tutorials