added second sort criteria for solveRec method