added a test for plan sorting and fixed a bug