1var sortByBits = function (arr) {2const map = {};34for (let n of arr) {5let counter = 0,6item = n;78while (item > 0) {9counter += item & 1; //increment counter if the lowest (i.e. the rightest) bit is 110item = item >> 1; //bitwise right shift (here is equivalent to division by 2)11}1213map[n] = counter;14}1516return arr.sort((a, b) => map[a] - map[b] || a - b); //sort by number of 1 bits; if equal, sort by value17};