I am trying to sort my vector of class pointers vector <Building*> company
. Below is my code
sort(company.begin(), company.end(), [] (Building* lhs, Building* rhs)
{
return (lhs->getCompanyName() > rhs->getCompanyName() && lhs->height() > rhs->height());
});
I am trying to sort based on two attributes in the class called getCompanyName()
and height()
. getCompanyName
is to be alphabetically order from A to Z while height
is to sorted in descending order.
The above sorting do not work.
However, if I split the two conditions up into the two below:
sort(company.begin(), company.end(), [] (Building* lhs, Building* rhs)
{
return lhs->getCompanyName() > rhs->getCompanyName();
});
sort(company.begin(), company.end(), [] (Building* lhs, Building* rhs)
{
return lhs->height() > rhs->height();
});
The vector will sort correctly but only based on one attribute.
Is there something wrong with my 2 attributes sorting code?
Edit:
getCompanyName()
will be sorted first based on A-Z.
Only after getCompanyName()
is sorted then height()
will be sorted based on descending order.
An example of the sorted expected output:
Company Name: AQA
Height: 300
Company Name: AFE
Height: 200
Company Name: BAC
Height: 600
Company Name: BFE
Height: 100
Company Name: CJE
Height: 1200
Aucun commentaire:
Enregistrer un commentaire