Using javascript sort()
method, I am trying to do sorting a list but sorting have in a group of even numbers and odd numbers.
The code which I tried is work
My try:
fiddle
var n = [10,20,21,4,5,6,7,99,0,-12,12,13,-45,10,20,21,-13,4,5,6,7,99,0,12,13,10,-99,20,21,4,5,6,7,99,0,12,13,10,20,21,4,5,6,7,99,-15,-18,0,12,13];
function sort(a, b) {
if (!(a % 2) && !(b % 2)) {
return a > b ? 1 : -1;
}
if ((a % 2) && (b % 2)) {
return a > b ? 1 : -1;
}
if ((a % 2) && !(b % 2)) {
return 1;
}
return -1;
}
console.log(n.sort(sort));