如何基于dd/mm/yyyy格式order_day字段对C++结构体数组排序?
按
order_day排序tech_info结构体数组的完整实现步骤 首先先修正现有代码的核心问题:你的sort_date函数返回类型错误、日期解析逻辑未关联到对应结构体,且缺少Date结构体定义。以下是完整的实现流程和代码:
步骤1:补充必要的结构体定义
首先定义Date结构体(你的compare函数依赖它),同时补全faults结构体(tech_info中用到但未定义):
#include <iostream> #include <string> #include <sstream> #include <algorithm> // 必须包含以使用std::sort // 补全faults结构体(根据你的业务需求调整成员) struct faults { int fault_id; std::string fault_desc; }; // 定义Date结构体,用于日期比较 struct Date { int day; int month; int year; }; // 你的tech_info结构体定义 typedef struct { int order_num; std::string order_day; // 用于排序的日期字段(dd/mm/yyyy) std::string client; std::string tech_type; int serial_key; long problem; std::string technician_name; std::string tech_fix; int price; int days_spent; std::string status; std::string order_type; int problems_num; faults problems[10]; }tech_info;
步骤2:实现日期字符串转Date的工具函数
将dd/mm/yyyy格式的字符串解析为Date对象,方便后续比较:
Date stringToDate(const std::string& dateStr) { Date date; std::stringstream ss(dateStr); std::string part; // 按/分割字符串,依次提取日、月、年 getline(ss, part, '/'); date.day = std::stoi(part); getline(ss, part, '/'); date.month = std::stoi(part); getline(ss, part, '/'); date.year = std::stoi(part); return date; }
步骤3:完善比较逻辑
保留你原有的Date比较函数,再封装一个直接比较tech_info指针的函数(适配std::sort的要求):
// 原有的Date比较函数 bool compare(const Date& d1, const Date& d2) { if (d1.year < d2.year) return true; if (d1.year == d2.year && d1.month < d2.month) return true; if (d1.year == d2.year && d1.month == d2.month && d1.day < d2.day) return true; return false; } // 直接比较两个tech_info指针的函数(供std::sort调用) bool compareTechInfo(const tech_info* a, const tech_info* b) { Date d1 = stringToDate(a->order_day); Date d2 = stringToDate(b->order_day); return compare(d1, d2); }
步骤4:修正排序函数
重新实现sort_date函数,直接使用std::sort对结构体指针数组排序:
void sort_date(tech_info* all_orders[], int counter) { // std::sort参数:数组首地址、数组尾地址、自定义比较函数 std::sort(all_orders, all_orders + counter, compareTechInfo); }
步骤5:调用排序函数
在业务代码中直接调用sort_date即可完成排序:
// 示例调用 int main() { // 初始化测试用的tech_info对象 tech_info order1 = {1, "01/05/2023", "ClientA", "TypeX", 123, 4567, "Tech1", "Fix1", 100, 2, "Done", "Repair", 1, {}}; tech_info order2 = {2, "15/03/2023", "ClientB", "TypeY", 456, 8901, "Tech2", "Fix2", 200, 1, "Done", "Replace", 1, {}}; tech_info order3 = {3, "01/05/2024", "ClientC", "TypeX", 789, 2345, "Tech3", "Fix3", 150, 3, "InProgress", "Repair", 2, {}}; tech_info* all_orders[] = {&order1, &order2, &order3}; int counter = 3; // 排序前输出 std::cout << "排序前:\n"; for (int i = 0; i < counter; i++) { std::cout << "订单" << all_orders[i]->order_num << " | 日期:" << all_orders[i]->order_day << "\n"; } // 执行排序 sort_date(all_orders, counter); // 排序后输出 std::cout << "\n排序后:\n"; for (int i = 0; i < counter; i++) { std::cout << "订单" << all_orders[i]->order_num << " | 日期:" << all_orders[i]->order_day << "\n"; } return 0; }
关键注意事项
- 必须包含
<algorithm>头文件才能使用std::sort; - 日期解析依赖
std::stoi,需确保输入的order_day格式严格为dd/mm/yyyy,若需要容错可添加异常捕获; - 排序操作直接修改指针数组的顺序,不会改变原
tech_info对象的内存位置。
内容的提问来源于stack exchange,提问作者radoslav pavlov
相关产品推荐
相关产品推荐

