Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sort short solution in Clear category for Sort Array by Element Frequency by new_hoschi
def frequency_sort(items):
## sort list by two keys:
## primary key is number the of appearances (- inverts the list, such that the sorted list starts with the element with the highest no of appearances
## secondary key applies when two elements are equally often in the list, the items.index(x) sorts by the first appearance in the original list as requested by the task
return sorted(items,key=lambda x: (-items.count(x),items.index(x)))
March 26, 2020
Comments: