Sort Faces Naked Edges by Direction

sort (167.0 KB)

Hi !!
I am trying to sort this faces regarding the edge of the surface they are sharing. I am somehow struggling with this .
I wrote this script that is not far from the answer.
Could someone help ?

Thnak you

assigned to grasshopper category