# 排序 升序排序: ```cpp void ascending(std::vector &x) { // #include std::sort(x.begin(), x.end()); } ``` 降序排序: ```cpp void descending(std::vector &x) { // #include // #include std::sort(x.begin(), x.end(), std::greater()); } ``` 自定义比较函数: ```cpp struct Data { std::string name; int age; }; /** * @brief 自定义比较函数 * * @param a 第一个待比较的值 * @param b 第二个待比较的值 * @return 返回为 true 时代表 a 应该放在 b 前面 */ bool compareFunc(Data &a, Data &b) { return a.name.length() > b.name.length(); } void customCompare(std::vector &x) { std::sort(x.begin(), x.end(), compareFunc); } ```