{"id":12843,"date":"2025-07-16T04:48:31","date_gmt":"2025-07-16T04:48:31","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12843"},"modified":"2025-07-16T04:48:31","modified_gmt":"2025-07-16T04:48:31","slug":"python-bool-sorting","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/python-bool-sorting","title":{"rendered":"Is Python&#8217;s Bool Sorting Defined?"},"content":{"rendered":"<h2>Is Python\u2019s Boolean Sorting Order Defined?<\/h2>\n<p>Yes, Python defines a clear and predictable sorting order for boolean values. This behavior is not an implementation quirk\u2014it\u2019s by design and documented.<\/p>\n<h3>Boolean Values in Python Are Subclasses of Integers<\/h3>\n<p>In Python, bool is a subclass of int. This means that False and True are treated as the integers 0 and 1, respectively:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nprint(isinstance(True, int))   # Output: True\r\nprint(False == 0)              # Output: True\r\nprint(True == 1)               # Output: True\r\n<\/pre>\n<h3>Sorting Behavior<\/h3>\n<p>Because of this inheritance, when you sort a list containing boolean values, they are sorted according to their integer values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nvalues = [True, False, True, False]\r\nprint(sorted(values))  # Output: [False, False, True, True]\r\n<\/pre>\n<p>This is deterministic and safe to rely on across Python versions. False will always come before True when sorted in ascending order.<\/p>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/what-is-python-used-for\" target=\"_blank\">Why Use Python<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Is Python\u2019s Boolean Sorting Order Defined? Yes, Python defines a clear and predictable sorting order for boolean values. This behavior is not an implementation quirk\u2014it\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12844,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-12843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12843"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12843"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12843\/revisions"}],"predecessor-version":[{"id":12845,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12843\/revisions\/12845"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12844"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}