Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Double sorted: first appearance, then popularity solution in Clear category for Sort Array by Element Frequency by grischenko.n
def frequency_sort(items):
popularity = dict(map(lambda x: (x, items.count(x)), items))
# Sort by first appearance in list
r = sorted(items, key=lambda x: items.index(x))
# Now resort by popularity, preserving first appearance
return sorted(r, key=lambda x: popularity[x], reverse=True)
March 6, 2020
Comments: