Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
key(element).update(index) solution in Clear category for Sort Array by Element Frequency by veky
import collections, dataclasses
@dataclasses.dataclass(order=True)
class Key:
desc_frequency: int = 0
first_index: int = None
def update(self, index):
self.desc_frequency -= 1
if self.first_index is None: self.first_index = index
def frequency_sort(a):
key = collections.defaultdict(Key).__getitem__
for index, element in enumerate(a): key(element).update(index)
return sorted(a, key=key)
Dec. 6, 2018