Counter() from Python's collections module is an unordered container, yet when built from same-size integers, the values() view appears as if the Counter was sorted by key first. This happens consistently both on my computer (3.4.3rc1), as well as online IDEs (tio.run, ideone.com) with Python versions up to and including 3.5; not the case with 3.6+ or PyPy.
from collections import Counter
import random
def counter_is_sorted(iterable):
c = Counter(iterable)
return list(c.values()) == [v for k, v in sorted(c.items())]
print(counter_is_sorted([random.randrange(1000) for _ in range(50000)]))
True
This is not always true when elements were updated and false for other types and mixed integer sizes:
print(counter_is_sorted([random.randrange(65000, 66000) for _ in range(50000)]))
print(counter_is_sorted([round(random.uniform(0, 10), 2) for _ in range(50000)]))
print(counter_is_sorted([random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(50000)]))
False
False
False
Is this by design or depending on some particular factors? Under what constraints can we safely assume the values() view to be sorted?