2 * @brief OmegaScript $sort function
4 /* Copyright (C) 2018 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
33 omegascript_sort(const vector
<string
>& args
,
36 const string
&list
= args
[0];
37 if (list
.empty()) return;
41 if (args
.size() > 1) {
42 for (auto opt_ch
: args
[1]) {
51 throw string("Unknown $sort option: ") + opt_ch
;
56 string::size_type split
= 0, split2
;
58 split2
= list
.find('\t', split
);
59 items
.emplace_back(list
, split
, split2
- split
);
61 } while (split2
!= string::npos
);
64 sort(items
.begin(), items
.end());
66 sort(items
.begin(), items
.end(),
67 [](const string
& a
, const string
& b
) {
72 value
.reserve(list
.size());
74 const string
* prev
= nullptr;
75 for (auto&& item
: items
) {
76 // Skip duplicates if "u" flag specified.
77 if (prev
&& *prev
== item
) {