The route solver already has the capabilities to find the best sequence. This is also called the
travelling salesman problem. As documented in the help page:
routeParams.findBestSequence=true;
routeParams.preserveFirstStop=false;
routeParams.preserveLastStop=false;
routeParams.returnStops = true;
Change the preserverFirstStop and the preserveLastStop to be true and the returnStops to be false so that you start and end at the first and last stop and everything else (best sequence) will be computed by the route solver.
Jay Sandhu