Yes, Python defines a clear and predictable sorting order for boolean values. This behavior is not an implementation quirk—it’s by design and documented.
In Python, bool is a subclass of int. This means that False and True are treated as the integers 0 and 1, respectively:
print(isinstance(True, int)) # Output: True print(False == 0) # Output: True print(True == 1) # Output: True
Because of this inheritance, when you sort a list containing boolean values, they are sorted according to their integer values:
values = [True, False, True, False] print(sorted(values)) # Output: [False, False, True, True]
This is deterministic and safe to rely on across Python versions. False will always come before True when sorted in ascending order.
Work with our skilled Python developers to accelerate your project and boost its performance.
Hire Python Developers