-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxll_array_sort.cpp
More file actions
56 lines (50 loc) · 1.65 KB
/
xll_array_sort.cpp
File metadata and controls
56 lines (50 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// xll_array_sort.cpp - Sort or partial sort arrays.
#include <algorithm>
#include "xll_array.h"
using namespace xll;
AddIn xai_array_sort(
Function(XLL_FP, "xll_array_sort", "ARRAY.SORT")
.Arguments({
Arg(XLL_FP, "array", "is an array or handle to an array to sort"),
Arg(XLL_LONG, "_count", "is an optional number of elements to partial sort. Default is 0."),
})
.FunctionHelp("Sort _count elements of array in increasing (_count >= 0) or decreasing (n < 0) order.")
.Category(CATEGORY)
.Documentation(R"xyzyx(
Sort all elements of <code>array</code> in increasing numerical order when
<code>_count = 0</code>.
If<code>_count = -1</code> sort in decreasing order. If <code>_count > 0</code> only sort
the smallest <code>_count</code> values. If <code>_count < -1</code> only sort
the largest <code>-_count</code> values.
If <code>array</code> is not a row or column then partial elements of
the last row are set to \(\infty\) or \(-\infty\) if the sort is
increasing or decreasing, respecively.
<p>
If <code>array</code> is a handle return a handle to the sorted array.
)xyzyx")
);
_FP12* WINAPI xll_array_sort(_FP12* pa, LONG n)
{
#pragma XLLEXPORT
FPX* _a = ptr(pa);
if (_a) {
pa = _a->get();
}
LONG na = (LONG)size(*pa);
n = std::clamp(n, -na, na);
if (n == 0) {
n = na;
std::sort(begin(*pa), end(*pa));
}
else if (n > 0) {
std::partial_sort(begin(*pa), begin(*pa) + n, end(*pa));
}
else if (n == -1) {
n = na;
std::sort(begin(*pa), end(*pa), std::greater<double>{});
}
else { // n < -1
std::partial_sort(begin(*pa), begin(*pa) - n, end(*pa), std::greater<double>{});
}
return pa;
}