I have a sort task, I need it to return the odd numbers first (starting from -9). Anyone have the quickest solution to this problem?

I have to sort an array of numbers to display odd numbers first(including negative numbers and duplicates.
At the moment, I have this array: var randomNumbers = [3, -3, 0, 1, 2, 6, 7, -8, -9, 9, 7, 5, 3, 1, 2, 9, 3, 4, 5, 6, 5, 7, -4, 1, -2, 3, 5, 6, 1, 4];
This function: randomNumbers.sort(function(a, b){
return Math.abs(a % 2) - Math.abs(b % 2) || a - b;
});
and the output is as follows: [-8, -4, -2, 0, 2, 2, 4, 4, 6, 6, 6, -9, -3, 1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 5, 5, 7, 7, 7, 9, 9]

As you can see it is returning the even numbers first, but I need it to return the odd numbers first (starting from -9). Anyone have the quickest solution to this problem?
I think I may need another variable. I would appreciate any help. Thank you!!!
You already invited:

Aamir

Upvotes from:

swap a and b so Math.abs(a % 2) - Math.abs(b % 2) becomes Math.abs(b % 2) - Math.abs(a % 2)

If you wanna answer this question please Login or Register