Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created October 26, 2020 13:42
Show Gist options
  • Save les-peters/304f0dec11e042fa8791d7c4a35d934f to your computer and use it in GitHub Desktop.
Save les-peters/304f0dec11e042fa8791d7c4a35d934f to your computer and use it in GitHub Desktop.
Sort by bits
import re
arr = [0,1,2,3,4,5,6,7,8]
arr.sort(key=lambda x: len(re.sub(r'0','', "{0:b}".format(x))))
print(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment