Don't have enough reputation to comment, so posting instead. In Python 3.10.12 doing
from collections import Counter
a = [1, 0, 1, 0]
s = sorted(a, key=Counter(a).get, reverse=True)
print(Counter(a).most_common())
print(a)
print(s)
gives
[(1, 2), (0, 2)]
[1, 0, 1, 0]
[1, 0, 1, 0]
So using sorted/sort might not give the expected results (I would have expected [0,0,1,1] or [1,1,0,0]). You might want to use another method of sorting for this use case. Doing
from collections import Counter
a = [1, 0, 1, 0]
d = [item for items, c in Counter(a).most_common() for item in [items] * c]
print(a)
print(d)
gives
[1, 0, 1, 0]
[1, 1, 0, 0]
You might need to sort again if the ordering of the sorted list is important.