Jack F
Jack F

Reputation: 553

sorting a vector with class items in it

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

Upvotes: 1

Views: 365

Answers (4)

Song Gao
Song Gao

Reputation: 2090

To better illustrate how to use std::sort with your scenario, here's a complete example with three comparison functions defined for three fields in your class:

#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>

class Item{
    public:
        float price;
        std::string name;
        float quantity;
        static bool SORT_BY_NAME(const Item & i, const Item & j) {
            return i.name.compare(j.name) < 0;
        }
        static bool SORT_BY_PRICE(const Item & i, const Item & j) {
            return i.price < j.price;
        }
        static bool SORT_BY_QUANTITY(const Item & i, const Item & j) {
            return i.quantity < j.quantity;
        }
        std::string ToString(){
            std::stringstream ss;
            ss << name << ": $" << price << "; " << quantity;
            return ss.str();
        }
};

int main(int argc, char ** argv) {
    std::vector<Item> items;
    Item item1, item2, item3;

    item1.name = "Name1";
    item1.price = 2;
    item1.quantity = 3;

    item2.name = "Name2";
    item2.price = 3;
    item2.quantity = 1;

    item3.name = "Name3";
    item3.price = 1;
    item3.quantity = 2;

    items.push_back(item1);
    items.push_back(item2);
    items.push_back(item3);


    std::sort(items.begin(), items.end(), Item::SORT_BY_NAME);
    std::cout<<std::endl<<"By name:"<<std::endl;
    for(std::vector<Item>::iterator i = items.begin(); i != items.end(); ++i)
        std::cout<<i->ToString()<<std::endl;

    std::sort(items.begin(), items.end(), Item::SORT_BY_PRICE);
    std::cout<<std::endl<<"By price:"<<std::endl;
    for(std::vector<Item>::iterator i = items.begin(); i != items.end(); ++i)
        std::cout<<i->ToString()<<std::endl;

    std::sort(items.begin(), items.end(), Item::SORT_BY_QUANTITY);
    std::cout<<std::endl<<"By quantity:"<<std::endl;
    for(std::vector<Item>::iterator i = items.begin(); i != items.end(); ++i)
        std::cout<<i->ToString()<<std::endl;

}

Upvotes: 2

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143119

std::sort algorithm has three parameters — first, last and comparator.

Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.

Upvotes: 0

emartel
emartel

Reputation: 7773

You simply need to write a comparison function for each field you want to sort on and depending on the user input select the right one.

See sort

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258618

You'd use a custom comparator object that either sorts according to name, price or quantity.

This is part of std::sort's functionality.

Upvotes: 1

Related Questions