Skip to content

Instantly share code, notes, and snippets.

@Cvetomird91
Last active November 16, 2020 19:14
Show Gist options
  • Save Cvetomird91/f5eedac2903e3fa428b59fb464cc7354 to your computer and use it in GitHub Desktop.
Save Cvetomird91/f5eedac2903e3fa428b59fb464cc7354 to your computer and use it in GitHub Desktop.
const nums = [7, 8, 6, 5];
binaryArray = nums.map((element) => {var decimal = Number(element).toString(2); return (decimal.length == 4 ? decimal : "0" + decimal);})
.sort((left, right) => {
var leftDigitCount = (left.match(/1/g) || []).length;
var rightDigitCount = (right.match(/1/g) || []).length;
return (leftDigitCount == rightDigitCount ? parseInt(left, 2) - parseInt(right, 2) : (leftDigitCount > rightDigitCount) ? 1 : -1)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment