Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:14:48+00:00 2026-06-14T03:14:48+00:00

lets say that i have a class that looks like this: Class Items{ private:

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:14:49+00:00Added an answer on June 14, 2026 at 3:14 am

    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;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a string that represents a date that looks like this:
Lets say that I have a HTML that looks like this: <div class=aaa><span>1</span></div> <div
Hello Lets say I have a console application, that looks like this class Program
Lets say I have a method that looks like this: public static String[] parseFoo(Foo
lets say I have a class like this: public class MyClass { private String
Let's say I have a very simple PrototypeJS class that looks like this: var
Lets say I have a class A that is fairly simple like this -
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
Lets say I have a JSON document / JavaScript object that looks like this:
Lets say I have a generator function that looks like this: def fib(): x,y

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.