Mantid
Loading...
Searching...
No Matches
SortTableWorkspace.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10
11namespace Mantid::DataHandling {
12
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(SortTableWorkspace)
18
19//----------------------------------------------------------------------------------------------
20
21
22int SortTableWorkspace::version() const { return 1; }
23
25const std::string SortTableWorkspace::category() const { return "Utility\\Sorting"; }
26
28const std::string SortTableWorkspace::summary() const { return "Sort a TableWorkspace."; }
29
30//----------------------------------------------------------------------------------------------
34 declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("InputWorkspace", "", Direction::Input),
35 "An input workspace.");
36 declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("OutputWorkspace", "", Direction::Output),
37 "An output workspace.");
38 declareProperty(std::make_unique<Kernel::ArrayProperty<std::string>>("Columns"), "Column names to sort by.");
39 declareProperty(std::make_unique<Kernel::ArrayProperty<int>>("Ascending"),
40 "List of bools for each column: true for ascending order, "
41 "false for descending. "
42 "If contains a single value it applies to all columns.");
43}
44
45//----------------------------------------------------------------------------------------------
49 API::ITableWorkspace_sptr ws = getProperty("InputWorkspace");
50 std::vector<std::string> columns = getProperty("Columns");
51 std::vector<int> ascending = getProperty("Ascending");
52
53 if (columns.empty()) {
54 throw std::invalid_argument("No column names given.");
55 }
56
57 // by default sort all columns in ascending order
58 if (ascending.empty()) {
59 ascending.emplace_back(1);
60 }
61
62 // if "Ascending" contains a single value - it's common for all columns.
63 if (ascending.size() == 1) {
64 int commonValue = ascending.front();
65 ascending.resize(columns.size(), commonValue);
66 } else if (ascending.size() != columns.size()) {
67 throw std::invalid_argument("Number of sorting options is different form number of columns.");
68 }
69
70 std::vector<std::pair<std::string, bool>> criteria(columns.size());
71 auto col = columns.begin();
72 auto asc = ascending.begin();
73 for (auto crt = criteria.begin(); crt != criteria.end(); ++crt, ++col, ++asc) {
74 crt->first = *col;
75 crt->second = (*asc) != 0;
76 }
77
78 API::ITableWorkspace_sptr outputWS(ws->clone());
79 outputWS->sort(criteria);
80 setProperty("OutputWorkspace", outputWS);
81}
82
83} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
A property class for workspaces.
SortTableWorkspace : TODO: DESCRIPTION.
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void init() override
Initialize the algorithm's properties.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54