Alternating panel types

I’m trying to change the types of panels of a sloped roof to be alternating along one of the grid lines, but the method I’m using (finding the centroid for each panel and sorting them along the x axis) is causing some inaccuracies in places where the panels are small, triangular, or where only part of a panel exists (check screenshot).

Now, I can work around this by filtering out these small solids by volume, and then only change them manually, or I can run this script on a roof with a regular shape, and then give it the shape I want after I get my panel types right.

Naturally I would like to try to avoid these workarounds since I have a lot of these roofs. So I was wondering if any of you had a better idea than my centroid sorting method?

Example file and script attached below for convenience.

Alternating panels .dyn (51.9 KB)
test.rvt (1.9 MB)