Last active
November 16, 2020 19:14
-
-
Save Cvetomird91/f5eedac2903e3fa428b59fb464cc7354 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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