- // It is critical that sorting is done with a stable algorithm
- // because all the values but the first are usually set to
- // -VALUE_INFINITE and we want to keep the same order for all
- // the moves but the new PV that goes to head.
- sort<RootMove>(Rml.begin() + MultiPVIteration, Rml.end());
-
- // In case we have found an exact score reorder the PV moves
- // before leaving the fail high/low loop, otherwise leave the
- // last PV move in its position so to be searched again.
- if (value > alpha && value < beta)
- sort<RootMove>(Rml.begin(), Rml.begin() + MultiPVIteration);
+ // Bring to front the best move. It is critical that sorting is
+ // done with a stable algorithm because all the values but the first
+ // and eventually the new best one are set to -VALUE_INFINITE and
+ // we want to keep the same order for all the moves but the new
+ // PV that goes to the front. Note that in case of MultiPV search
+ // the already searched PV lines are preserved.
+ sort<RootMove>(Rml.begin() + MultiPVIdx, Rml.end());
+
+ // In case we have found an exact score and we are going to leave
+ // the fail high/low loop then reorder the PV moves, otherwise
+ // leave the last PV move in its position so to be searched again.
+ // Of course this is needed only in MultiPV search.
+ if (MultiPVIdx && value > alpha && value < beta)
+ sort<RootMove>(Rml.begin(), Rml.begin() + MultiPVIdx);