lets say that i have a class that looks like this:
Class Items{
private:
float price;
string name;
float qunatity;
public:
getname(string nam){name=nam;}}
etc…
and i have a vector consisting of this class items, how would i then make it to sort the vector according to the users input like if the user wants to sort the items by name then it would sort by name etc.
EDIT::
ok so i have a class items and also have a class inventory:
Class Inventory{
print();
getdata();
sort();
static bool SORT_BY_NAME(const Item& i, const Item &j)}
then i have a function that Sang Geo wrote for the comparison
static bool Inventory::SORT_BY_NAME(const Item & i, const Item & j) {
return i.name.compare(j.name) < 0;
}
and then i also have a sort function that will use different bool sort functions
void Inventory::sorting(){
int x;
cout<<"How do you want to sort it: 1.name 2.ID 3.month";
cin>>x;
// vector<Item>::iterator it;
switch(x){
case 1:
std::sort(items.begin(), items.end(), Inventory::SORT_BY_NAME);
}
but it says that Items::name is private
To better illustrate how to use
std::sortwith your scenario, here’s a complete example with three comparison functions defined for three fields in your class: