Skip to content

Instantly share code, notes, and snippets.

@SirPhemmiey
Forked from Samuelachema/mysort.js
Created December 23, 2018 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirPhemmiey/c90a9ce522046611ca7cd1d1d8f4b27b to your computer and use it in GitHub Desktop.
Save SirPhemmiey/c90a9ce522046611ca7cd1d1d8f4b27b 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