From ad30b64474f17a28ab91ec23caa8ac2ef54650fe Mon Sep 17 00:00:00 2001
From: Samuel Jones <samjones714@gmail.com>
Date: Tue, 21 Aug 2018 16:24:00 +0100
Subject: [PATCH] Re #22896 Implement SortXAxis in Stitch1D

---
 Framework/Algorithms/src/Stitch1D.cpp | 36 ++++++++-------------------
 1 file changed, 10 insertions(+), 26 deletions(-)

diff --git a/Framework/Algorithms/src/Stitch1D.cpp b/Framework/Algorithms/src/Stitch1D.cpp
index 2dfbd76a3e2..bf03e917e36 100644
--- a/Framework/Algorithms/src/Stitch1D.cpp
+++ b/Framework/Algorithms/src/Stitch1D.cpp
@@ -3,6 +3,7 @@
 #include "MantidAPI/MatrixWorkspace.h"
 #include "MantidAPI/WorkspaceProperty.h"
 #include "MantidAlgorithms/RunCombinationHelpers/RunCombinationHelper.h"
+#include "MantidAlgorithms/SortXAxis.h"
 #include "MantidHistogramData/HistogramDx.h"
 #include "MantidHistogramData/HistogramE.h"
 #include "MantidHistogramData/HistogramX.h"
@@ -41,32 +42,11 @@ bool isNonzero(double i) { return (0 != i); }
  @return A shared pointer to the resulting MatrixWorkspace
  */
 void sortXAxis(MatrixWorkspace_sptr &ws) {
-  // using a std::multimap (keys are sorted)
-  std::multimap<double, double> sorter;
-  double x_value;
-  const int nHists = static_cast<int>(ws->getNumberHistograms());
-  for (int i = 0; i < nHists; ++i) {
-    for (size_t k = 0; k < ws->size(); ++k) {
-      x_value = ws->x(i)[k];
-      sorter.insert(std::pair<double, double>(x_value, ws->y(i)[k]));
-      sorter.insert(std::pair<double, double>(x_value, ws->e(i)[k]));
-      // histograms: no recalculation of Dx implemented -> skip
-      if (ws->hasDx(i) && !ws->isHistogramData())
-        sorter.insert(std::pair<double, double>(x_value, ws->dx(i)[k]));
-    }
-    int l = 0;
-    auto it = sorter.cbegin();
-    while (it != sorter.cend()) {
-      ws->mutableX(i)[l] = it->first;
-      ws->mutableY(i)[l] = it->second;
-      ws->mutableE(i)[l] = (++it)->second;
-      // histograms: no recalculation of Dx implemented -> skip
-      if (ws->hasDx(i) && !ws->isHistogramData())
-        ws->mutableDx(i)[l] = (++it)->second;
-      ++l;
-      ++it;
-    }
-  }
+  Mantid::Algorithms::SortXAxis alg;
+  alg.initialize();
+  alg.setProperty("InputWorkspace", ws);
+  alg.setProperty("OutputWorkspace", "outputSearch");
+  alg.execute();
 }
 } // namespace
 
@@ -658,6 +638,10 @@ void Stitch1D::exec() {
     if (!result)
       g_log.error("Could not retrieve joined workspace.");
     sortXAxis(result);
+    MatrixWorkspace_sptr outputSearch =
+        boost::dynamic_pointer_cast<MatrixWorkspace>(
+            AnalysisDataService::Instance().retrieve("outputSearch"));
+    result = outputSearch;
   }
   setProperty("OutputWorkspace", result);
   setProperty("OutScaleFactor", m_scaleFactor);
-- 
GitLab