Sort the shapes before solving.