Results 1 to 5 of 5

Thread: How to sort vector objects by class member variables in c++?

  1. #1
    Join Date
    Nov 2009
    Posts
    57

    How to sort vector objects by class member variables in c++?

    Hello to all,
    I don't know whether it is right place or not. I want to sort vector objects by class member variables in c++? I tried various method but none of them worked out. Can anyone tell me how to fix this problem.
    Thanks in advanced.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to sort vector objects by class member variables in c++?

    With following program you can easily sort vector objects by class member variables. It is very easy process. Just try to understand it. I have use 3 for loop to solve this problem.



    Code:
     
          class tropicalfruit 
     
          {
     
          public:
       
          string names;
       
          double prices;
      
          };
    
           bool sortByNames(tropicalfruits &As, tropicalsfruit &Bs) 
      
          {
      
          return (As.names < Bs.names);
    
          }
    
           
    
          bool sortByPricse(tropicalfrusit &As, tropicalfsruit &sB) 
    
          {
    
          return (As.prices < Bs.prices);
    
          }
    
           
    
          int main()
    
          {
    
          int sortchoices;
    
    
    
          string fruitnames[] = {"dhdbhdfh", "mhghg", "waa", "kghcv", "jrcfx", "jtdfz", "zdgss", "xvsds", "sdesfes", "sgfses"};
    
          double fruitprices[] = {2.8, 4.4, 7.6, 5.4, 6.8, 8.5, 3.4, 7.87, 7.6, 73.73};
    
           
    
          vector<tropicalfruit> fruitlists; 
    
          tropicalfruits fruits;
           
    
          for(int k=0; k<10; k++) 
    
          {
    
          fruits.names = fruitname[k];
    
          fruits.prices = fruitprice[k];
    
           
    
          fruitlists.push_back(fruits);
    
          }
    
           
    
          for(int k=0; k<10; k++) 
          {
    
          cout << fruitlist[k].name << " " << fruitlists[k].price << endl;
    
          }
    
          cout << endl;
    
           
    
          cout<<"Arrange fruits by 1)Price or 2)Name : "; 
          if(sortchoices == 1)
    
          {
    
          fruitlists.sort(sortByNames);
    
          }
    
          else if(sortchoices == 2)
    
          {
    
          fruitlists.sort(sortByPrices);
    
          }
    
           
    
          for(int k=0;k<10; k++) 
    
          {
    
          cout << fruitlists[k].name << " " << fruitlists[k].price << endl;
    
          }
    
          cout << endl;
    
          }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to sort vector objects by class member variables in c++?

    It seems that there is some problem in your code. You have to use following code to fix this problem.


    Code:
          class tropicalfruits
     
          {
      
          public:
       
          string names;
       
          double prices;
      
          };
    The class tropicalfruits is used to combine all types of fruits and there various different features.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to sort vector objects by class member variables in c++?

    Code:
          bool sortByName(tropicalfruits &As, tropicalfruits &Bs)
     {
     return (As.names < Bs.names);
     }
     bool sortByPrice(tropicalfruits &As, tropicalfruits &Bs) 
    {
     return (As.prices < Bs.prices);
      }
    You have to do some comparison between two object like above program to fix this problem. It is very easy process. You have to use void functions and have to pass reference to your fruitlists vector object.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to sort vector objects by class member variables in c++?

    Code:
     
          {
    
          fruitlist.sort(sortByNames); 
          }
    
          else if(sortchoices == 3)
    
          {
    
          fruitlists.sort(sortByPrices); 
    
          }

    You have to use above code in your project to sort vector objects by class member variables. It is very easy process. Above program says that "class std::vectors<tropicalfruits, std::allocators<tropicalfruist> >" has no member named 'sort'. You have to use .sort() function to fix this problem.

Similar Threads

  1. Polymorphism and objects stored in a vector
    By New ID in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 09:45 AM
  2. Vector objects and methods
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 04:14 AM
  3. How to sort points_vec vector in c++?
    By kamina23 in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 04:31 PM
  4. Sort vector of 3D point by X axis
    By Abdullah30 in forum Software Development
    Replies: 4
    Last Post: 07-12-2009, 07:09 PM
  5. How to pass reference to vector objects
    By Recko in forum Windows Software
    Replies: 3
    Last Post: 07-08-2009, 10:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,855,792.80795 seconds with 16 queries