i am currently learning how to program using javascript, i came across a problem ( Write function that sort an array with odd first and even later // sortArray([3,24,1,4,9,10]) => [1,3,9,4,10,24] // sortArray([2,1,4,9,3,3,10,12]) => [1,3,3,9,2,4,10,12]), i have implemented a solution making use of the sort function and multiple variables. See code below;
function sortingArr (array) {
let sortedArr = (array.sort(function(a, b) { return a - b; })), oddArr = [], evenArr = [];
for (i = 0; i < array.length; i++){
if (sortedArr[i] % 2 == 0){
evenArr.push(sortedArr[i]);
}
else {
oddArr.push(sortedArr[i]);
}
}
return oddArr.concat(evenArr);
}
console.log (sortingArr ([2,1,4,9,3,3,10,12]));
I would appreciate suggestions I can use to solve the same problem with minimal memory allocation and runtime.