
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sorting odd and even elements separately JavaScript
We are required to write a JavaScript function that takes in an array of Integers.
The function should sort the array such all the odd numbers come first, then followed by the even number.
The order of odd or even numbers within themselves is not of much importance, but all odd numbers should come before any even number.
For example −
If the input array is −
const arr = [0, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 1];
Then the sorted array should be (it’s one of the many solutions where all odds are before evens) −
const output = [ 1, 3, 5, 7, 9, 1, 0, 2, 4, 6, 8, 0 ];
Example
const arr = [0, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 1]; const sortOddEven = (arr = []) => { let i = 0, j, temp; while (i < arr.length - 1) { j = i; while (!(arr[j] % 2) && arr[j + 1] % 2) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; if (!j) { break; }; j--; }; i++; }; }; sortOddEven(arr); console.log(arr);
Output
And the output in the console will be −
[ 1, 3, 5, 7, 9, 1, 0, 2, 4, 6, 8, 0 ]
- Related Articles
- JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
- Sorting parts of array separately in JavaScript
- Separate odd and even in JavaScript
- Swap Even Index Elements And Odd Index Elements in Python
- Odd even index difference - JavaScript
- Count subarrays with same even and odd elements in C++
- Add class (odd and even) in HTML via JavaScript?
- Swapping even and odd index pairs internally in JavaScript
- Missing even and odd elements from the given arrays in C++
- Separate Odd and Even Elements into Two Separate Arrays in Java
- Adding only odd or even numbers JavaScript
- Find even odd index digit difference - JavaScript
- Odd even sort in an array - JavaScript
- Count number of even and odd elements in an array in C++
- Absolute Difference of even and odd indexed elements in an Array (C++)?

Advertisements