1 #ifndef STAN_MATH_PRIM_FUN_SORT_DESC_HPP
2 #define STAN_MATH_PRIM_FUN_SORT_DESC_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 #include <algorithm>
8 #include <functional>
9 #include <vector>
10 
11 namespace stan {
12 namespace math {
13 
14 /**
15  * Return the specified standard vector in descending order.
16  *
17  * @tparam T Type of elements contained in vector.
18  * @param xs Vector to order.
19  * @return Vector in descending order.
20  * @throw std::domain_error If any of the values are NaN.
21  */
22 template <typename T>
sort_desc(std::vector<T> xs)23 inline std::vector<T> sort_desc(std::vector<T> xs) {
24   check_not_nan("sort_asc", "container argument", xs);
25   std::sort(xs.begin(), xs.end(), std::greater<T>());
26   return xs;
27 }
28 
29 /**
30  * Return the specified vector in descending order.
31  *
32  * @tparam EigVec type of the vector
33  *
34  * @param xs Vector to order.
35  * @return Vector in descending order.
36  * @throw std::domain_error If any of the values are NaN.
37  */
38 template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
sort_desc(EigVec && xs)39 inline plain_type_t<EigVec> sort_desc(EigVec&& xs) {
40   plain_type_t<EigVec> x = std::forward<EigVec>(xs);
41   check_not_nan("sort_asc", "container argument", x);
42   std::sort(x.data(), x.data() + x.size(),
43             std::greater<value_type_t<EigVec>>());
44   return x;
45 }
46 
47 }  // namespace math
48 }  // namespace stan
49 
50 #endif
51