Given a list containing only integers 0, 1, and 2 (representing the colors red, white, and blue), sort the list so that all 0s come first, followed by all 1s, and then all 2s. Return the sorted list. Return null if the input is null.

Example 1:

Input: nums = [2, 0, 2, 1, 1, 0] Output: [0, 0, 1, 1, 2, 2] Explanation: Sorted with all 0s first, then 1s, then 2s

Example 2:

Input: nums = [0, 1, 2] Output: [0, 1, 2] Explanation: Already sorted, no changes needed

Example 3:

Input: nums = [1, 1, 1] Output: [1, 1, 1] Explanation: All same color, order is unchanged

Example 4:

Input: nums = null Output: null
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.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In