from functools import cmp_to_key
def comp(x, y):
return x - y if arr.count(x) == arr.count(y) else arr.count(y) - arr.count(x)
arr = [7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 8, 8]
# Works because sorted is an iterable which doesn't modify the original array
print(sorted(arr, key = cmp_to_key(comp)))
# Broken because this sorts the array in-place
arr.sort(key = cmp_to_key(comp))
print(arr)
ZnJvbSBmdW5jdG9vbHMgaW1wb3J0IGNtcF90b19rZXkKCmRlZiBjb21wKHgsIHkpOgoJcmV0dXJuIHggLSB5IGlmIGFyci5jb3VudCh4KSA9PSBhcnIuY291bnQoeSkgZWxzZSBhcnIuY291bnQoeSkgLSBhcnIuY291bnQoeCkKCmFyciA9IFs3LCA1LCA3LCAzLCAzLCA1LCAzLCAzLCAwLCAzLCAxLCAxLCA1LCAxLCAxLCAxLCAyLCAyLCA4LCA1LCA4LCA4XQoKIyBXb3JrcyBiZWNhdXNlIHNvcnRlZCBpcyBhbiBpdGVyYWJsZSB3aGljaCBkb2Vzbid0IG1vZGlmeSB0aGUgb3JpZ2luYWwgYXJyYXkKcHJpbnQoc29ydGVkKGFyciwga2V5ID0gY21wX3RvX2tleShjb21wKSkpCgojIEJyb2tlbiBiZWNhdXNlIHRoaXMgc29ydHMgdGhlIGFycmF5IGluLXBsYWNlCmFyci5zb3J0KGtleSA9IGNtcF90b19rZXkoY29tcCkpCnByaW50KGFycik=
[1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8, 8, 8, 2, 2, 7, 7, 0]
[0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 7, 7, 8, 8, 8]
Warning: cannot find your CPU L2 cache size in /proc/cpuinfo