1 #ifndef STAN__MATH__MATRIX__SORT_INDICES_HPP
2 #define STAN__MATH__MATRIX__SORT_INDICES_HPP
24 template <
bool ascending,
typename C>
25 class index_comparator {
34 index_comparator(
const C& xs) :
xs_(xs) { }
44 bool operator()(
int i,
int j)
const {
46 return xs_[i-1] <
xs_[j-1];
48 return xs_[i-1] > xs_[j-1];
63 template <
bool ascending,
typename C>
64 std::vector<int> sort_indices(
const C& xs) {
65 typedef typename index_type<C>::type idx_t;
66 idx_t
size = xs.size();
67 std::vector<int> idxs;
69 for (idx_t i = 0; i <
size; ++i)
71 index_comparator<ascending,C> comparator(xs);
72 std::sort(idxs.begin(), idxs.end(), comparator);
87 return sort_indices<true>(xs);
99 return sort_indices<false>(xs);
std::vector< int > sort_indices_asc(const C &xs)
Return a sorted copy of the argument container in ascending order.
int size(const std::vector< T > &x)
std::vector< int > sort_indices_desc(const C &xs)
Return a sorted copy of the argument container in ascending order.