/
sortvector.cpp
41 lines (37 loc) · 988 Bytes
/
sortvector.cpp
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
#include "sortvector.h"
SortVector::SortVector(const VectorXf& vector, const SortType& type)
{
mSize = vector.size();
for (int i = 0; i < mSize; i++) {
mStdVector.push_back(ElementWithIndex(vector(i), i));
}
if (type == ascend) {
sort(mStdVector.begin(), mStdVector.end(), ascendComparator);
} else {
sort(mStdVector.begin(), mStdVector.end(), descendComparator);
}
mIndices = VectorXi(mSize);
mVector = VectorXf(mSize);
for (int i = 0; i < mSize; i++) {
mVector(i) = mStdVector[i].first;
mIndices(i) = mStdVector[i].second;
}
}
bool SortVector::descendComparator(const ElementWithIndex& a,
const ElementWithIndex& b)
{
if (a.first > b.first) {
return true;
} else {
return false;
}
}
bool SortVector::ascendComparator(const ElementWithIndex& a,
const ElementWithIndex& b)
{
if (a.first < b.first) {
return true;
} else {
return false;
}
}