Sort Integers by The Number of 1 Bits

class Solution:
    def sortByBits(self, arr: List[int]) -> List[int]:
        
        cache = {}
        for n in arr:
            temp = 0
            t = n
            while n:
                temp = temp + (n % 2)
                n = n // 2
            if temp not in cache:
                cache[temp] = []
            cache[temp].append(t)

        out = []
        for n in sorted(cache):
            out.extend(sorted(cache[n]))
        return out