Sort by Number of Set Bits

Sort an integer array by the number of set bits (1 bits) in each element's binary representation. Elements with the same popcount should be sorted by their numeric value in ascending order. Return the sorted array.

Examples
Input: [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
Hints

Sort by Number of Set Bits

Sort an integer array by the number of set bits (1 bits) in each element's binary representation. Elements with the same popcount should be sorted by their numeric value in ascending order. Return the sorted array.