Skip to content

Instantly share code, notes, and snippets.

@nccharles
Forked from Samuelachema/mysort.js
Created May 12, 2019 12:06
Show Gist options
  • Save nccharles/6e0ed873e3645841bf3451c2cda631de to your computer and use it in GitHub Desktop.
Save nccharles/6e0ed873e3645841bf3451c2cda631de to your computer and use it in GitHub Desktop.
Write a mySort function which takes in an array integers, and should return an array of the inputed integers sorted such that the odd numbers come first and even numbers come last.
/*
JavaScript
Write a mySort function which takes in an array integers, and should return an array of the inputed integers sorted such that the odd numbers come first and even numbers come last.
For exampl1e:
mySort( [90, 45, 66, 'bye', 100.5] )
should return
[45, 66, 90, 100]
*/
function mySort(nums) {
let evens = [];
let odds = [];
for (let i = 0; i < nums.length; i++) {
if(typeof nums[i] === "number"){ // ignore if its not a number
if ((nums[i] % 2) === 1) {
odds.push(parseInt(nums[i]));
}
else {
evens.push(parseInt(nums[i]));
}
}
}
let numsArray = odds.sort((a, b) => a - b).concat(evens.sort((a, b) => a - b));
return numsArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment