Exercise 1: Three Siblings, Sorted Purely by z-index — Possible Solution ==================================================================== THE TEST ------------------------------ s1 = block({'background-color': 'red', 'z-index': '2'}) s2 = block({'background-color': 'green', 'z-index': '0'}) s3 = block({'background-color': 'blue', 'z-index': '1'}) container = block({}, children=[s1, s2, s3]) # source order: red, green, blue layout_block(container, root_cb) [cmd.color for cmd in build_display_list_zindex(container)] RESULT ------------------------------ ['green', 'blue', 'red'] Sorted purely by z-index ascending (0, 1, 2) -- green (z=0) paints first, blue (z=1) second, red (z=2) last (on top) -- completely independent of the original source order (red, green, blue). WHY THE RESULT IS SORTED BY Z-INDEX, NOT SOURCE POSITION ------------------------------ build_display_list_zindex's own relevant line is: ordered_children = sorted(layout_box.children, key=get_z_index) sorted() with a key function reorders the input list according to whatever get_z_index() returns for each element -- it does NOT preserve the original list's own order except among elements whose key values are equal (Python's stable-sort guarantee). Here, all three z-index values (2, 0, 1) are genuinely distinct, so there are no ties to preserve -- the output order is determined ENTIRELY by the numeric comparison of z-index values: 0 < 1 < 2, giving [s2, s3, s1] regardless of the fact that s1 appeared first in the original children list. WHY THIS WORKS AS AN ANSWER ------------------------------ This confirms z-index is a genuine override of source order, not merely a tie-breaker that only matters when source order is somehow ambiguous. The chapter's own two-sibling example already demonstrated this with two boxes; testing three, with z-index values deliberately NOT matching either ascending or descending source order (2, 0, 1 rather than, say, 0, 1, 2 which could coincidentally match a "reverse source order" pattern), confirms the sort genuinely keys off z-index alone -- there's no hidden dependency on the boxes' own original positions sneaking into the result.