Given a list of integers, return a new list with all even numbers appearing first in their original relative order, followed by all odd numbers in their original relative order. Return null for null input and an empty list for empty input.
Examples:
Input: nums = [1, 2, 3, 4, 5]
Output: [2, 4, 1, 3, 5]
Explanation: Evens [2, 4] come first, then odds [1, 3, 5]
Input: nums = [3, 1, 5]
Output: [3, 1, 5]
Explanation: No even numbers, so the list is returned with odds only
Input: nums = [2, 4, 6]
Output: [2, 4, 6]
Explanation: All numbers are even, so the list is returned unchanged
Input: nums = []
Output: []
Explanation: Empty list returns empty list
Input: nums = [7]
Output: [7]
Explanation: Single odd number
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay September 2026 (Sep 1–15)Sponsored by FLXBL
Contest runs September 1 - 15. Complete challenges to climb the leaderboard!
1st & 2nd: Salesforce certification exam voucher — see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.