Is Python’s Boolean Sorting Order Defined?

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.

Boolean Values in Python Are Subclasses of Integers

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

Sorting Behavior

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.

Also Read

Also Read:

Why Use Python

Need Help With Python Development?

Work with our skilled Python developers to accelerate your project and boost its performance.

Hire Python Developers

Support On Demand!

Related Q&A