用sort如何对一个二维数组排序,数组元素都是结构体;
我想要实现records的第二维数据按照time进行排序,如何书写sort的第三个参数
struct month
{
string time;
string status;
};
struct customer
{
double total_amount;
vector<vector<month>> records;
};
bool cmp(const month &a, const month &b)
{
if (a.time < b.time)
return 1;
return 0;
}
customer data[100];
sort(data[0].records[0].begin(),data[0].records[0].end(),cmp);
// 结果显示data[0].records[0]这个数组的元素没有按照time从小到大排序
如果我用以下代码则可以实现
vector<month>temp = data[0].records[0];
sort(temp.begin(),temp.end(),cmp);
data[0].records[0] = temp;
这是为啥=-=
sort函数不可以直接对结构体类型进行排序吧?
而且sort(data[0].begin(),data[0].end(),cmp); 这个是对data[0]元素排序,而不是data整个结构体数组
sort(temp.begin(),temp.end(),cmp);这个可以运行是因为对vector数据类型进行排序,此时符合语法规定
一些个人见解,我也是正在学习这部分的知识。如果有错误,希望不会误导你。