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 August 2026

Contest runs August 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 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