You are given an integer array arr. Sort the integers in the array&nbsp;in ascending order by the number of 1's&nbsp;in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

Return the array after sorting it.

&nbsp;
Example 1:

Input: arr = [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
Explantion: [0] is the only integer with 0 bits.
[1,2,4,8] all have 1 bit.
[3,5,6] have 2 bits.
[7] has 3 bits.
The sorted array by bits is [0,1,2,4,8,3,5,6,7]


Example 2:

Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
Output: [1,2,4,8,16,32,64,128,256,512,1024]
Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.


&nbsp;
Constraints:


	1 &lt;= arr.length &lt;= 500
	0 &lt;= arr[i] &lt;= 104


class Solution:
    def sortByBits(self, arr: List[int]) -> List[int]:
        binary = []
        final = []
        arr.sort()
        for i in arr:
            binary.append(bin(i).count("1"))
        for i,j in zip(arr,binary):
            final.append((i,j))
        z = sorted(final, key=lambda x:x[1])
        
        ls = []
        for k in z:
            ls.append(k[0])
        
        return ls
class Solution {
    public int[] sortByBits(int[] arr) {

        Integer[] arrInt = new Integer[arr.length];

        for(int i=0;i<arr.length;i++) {
            arrInt[i]=arr[i];
        }

        Arrays.sort(arrInt, new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                int aBits=numOfBits(a);
                int bBits=numOfBits(b);
                if(aBits==bBits) {
                    return a-b;
                }
                return aBits-bBits;
            }
        });

        for(int i=0;i<arr.length;i++) {
            arr[i]=arrInt[i];
        }
        return arr;
    }

    public int numOfBits(int a) {
        int bits=0;
        while(a!=0) {
            bits+=a&1;
            a=a>>>1;
        }

        return bits;
    }
}
class Solution {
public:
    vector<int> sortByBits(vector<int>& arr) {
        int n = size(arr);
        priority_queue<pair<int, int>> pq;
        
        for(auto &x : arr) {
            int count = 0;
            int a = x;
            while(a) {
                count += a & 1;
                a >>= 1;
            }
            pq.push({count, x});
        }
        n = n - 1;
        while(!pq.empty()) {
            arr[n--] = pq.top().second;
            pq.pop();
        }
        
        return arr;
    }
};
var sortByBits = function(arr) {
    const map = {};
    
    for (let n of arr) {
        let counter = 0, item = n;
        
        while (item > 0) {
			counter += (item & 1);    //increment counter if the lowest (i.e. the rightest) bit is 1
			item = (item >> 1);        //bitwise right shift (here is equivalent to division by 2)
        }
        
        map[n] = counter;
    }

    return arr.sort((a, b) => map[a] - map[b] || a - b) //sort by number of 1 bits; if equal, sort by value
};
Sort Integers by The Number of 1 Bits