We’re preparing your current view and syncing the latest data.
Given an array of integers, sort the array in ascending order according to the number of 1 bits in their binary representation. If two numbers have the same number of 1 bits, they should be sorted by their numeric value.
An array of integers nums.
Return the array sorted according to the criteria described.
1 <= nums.length <= 500 0 <= nums[i] <= 10^4
Example 1
Input
[0,1,2,3,4,5,6,7,8]
Output
[0,1,2,4,8,3,5,6,7]
Explanation
Numbers sorted first by the count of '1's in their binary form, then by numerical value.