ECMAScript 6, 6861
Assumes z is the input
z.sort((a,b)=>{forc=d=e=0;while(c=d=e=0;++c<32;++c<32){d+=a>>c&1;e+=b>>c&1d+=a>>c&1,e+=b>>c&1}return ,e-d}) Test data
[28943,825,11746,16375,32425,19944,21826,15752,15342,3944,28436].sort( (a,b)=>{ for(c=d=e=0;++c<32;){ c=d=e=0; d+=a>>c&1;e+=b>>c&1while(++c<32) } returnd+=a>>c&1,e+=b>>c&1 },e-d }) [16375, 15342, 32425, 11746, 19944, 28436, 28943, 15752, 3944, 21826, 825] Thanks, toothbrush for the shorter solution.