Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Counter most_common solution in Clear category for Sort Array by Element Frequency by Striga
from collections import Counter
def frequency_sort(items):
return [i for i, c in Counter(items).most_common() for i in [i] * c]
if __name__ == '__main__':
assert list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])) == [4, 4, 4, 4, 6, 6, 2, 2]
assert list(frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob'])) == ['bob', 'bob', 'bob', 'carl', 'alex']
assert list(frequency_sort([17, 99, 42])) == [17, 99, 42]
assert list(frequency_sort([])) == []
assert list(frequency_sort([1])) == [1]
Oct. 26, 2020
Comments: