Back to all solutions

#905 - Sort Array By Parity

Problem Description

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.

Solution

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var sortArrayByParity = function(nums) {
  return nums.sort((a, b) => a % 2 === 0 ? -1 : 1);
};