Implemented nested sort