class: sort coutput by element (member) value

Hello!
Please, is it possible to sort this output by ID value, so that we put ID value in a loop? How would be the syntax?
Many thanks!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;


class product {
   
  public:
    int ID;
    string name;
    string price;
    void set_values(int, string, string);
};

void product::set_values (int a, string b, string c) {
  ID = a;
  name = b;
  price=c;
}


int main(){    

  product toy;
  product book;
  product dress;

  toy.set_values(1, "Toy", "11.22€");
  book.set_values(2, "Book", "22.33€");
  dress.set_values(3, "Dress", "33.44€");

     cout<<toy.ID<<endl<<toy.name<<endl<<toy.price<<endl<<endl<<book.ID<<endl<<book.name<<endl<<book.price<<endl<<endl<<dress.ID<<endl<<dress.name<<endl<<dress.price<<endl;
return 0;

}


Output:

1
Toy
11.22€

2
Book
22.33€

3
Dress
33.44€
Last edited on
Put your products in container and sort them using standard algorithm and your own comparsion function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class product
{
  public:
    int ID;
    std::string name;
    std::string price;
    product() = default;
    product(int id, std::string name_, std::string price_) :
        ID(id), name(name_), price(price_) {};
    void set_values(int, std::string, std::string);
};

void product::set_values(int a, std::string b, std::string c)
{
    ID = a;
    name = b;
    price=c;
}

bool sortByName(const product& lhs, const product& rhs)
{
    return lhs.name < rhs.name;
}

int main()
{
    std::vector<product> products {{1, "Toy", "11.22€"}, {2, "Book", "22.33€"},
                                   {3, "Dress", "33.44€"},                     };
    std::cout << "Sort by ID using lambdas\n";
    std::sort(products.begin(), products.end(), [](const product& lhs, const product& rhs)
                {
                    return lhs.ID < rhs.ID;
                });
    for(const auto& val: products)
        std::cout << val.ID    << '\n' <<
                     val.name  << '\n' <<
                     val.price << '\n' << std::endl;
    std::cout << "Sort by name using comparsion function\n";
    std::sort(products.begin(), products.end(), sortByName);
    for(const auto& val: products)
        std::cout << val.ID    << '\n' <<
                     val.name  << '\n' <<
                     val.price << '\n' << std::endl;
}

One way is to put your products in a vector container and use sort to sort them by ID.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

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

using namespace std;


class Product {

	public:
		int ID;
		string name;
		string price;
		Product(int myId, string MyProduct, string myPrice) : ID(myId), name(MyProduct), price(myPrice) {}

};

bool sortId(const Product &val1, const Product &val2) { return val1.ID < val2.ID; }

int main(){

	// our Vector
	vector<Product> MyProducts;

	// populate some data in a random order of id
	MyProducts.push_back(Product(9, "Toy", "11.22"));
	MyProducts.push_back(Product(8, "Book", "22.33"));
	MyProducts.push_back(Product(5, "Dress", "33.44"));
	MyProducts.push_back(Product(7, "Car", "1116.22"));
	MyProducts.push_back(Product(6, "Bike", "148.33"));
	MyProducts.push_back(Product(2, "Phone", "99.44"));
	MyProducts.push_back(Product(1, "Calculator", "19.22"));
	MyProducts.push_back(Product(3, "Pen", "1.44"));
	MyProducts.push_back(Product(4, "Blu-Ray", "199.44"));

	// display list before sorting.
	cout << endl <<  "*** Before Sorting ***" << endl;
	for (int i = 0; i < 9; i++)
		cout << "ID: " << MyProducts[i].ID << ", Name: " << MyProducts[i].name << ", Price: $" << MyProducts[i].price << endl;

	// sort the list in ID order.
	sort(MyProducts.begin(), MyProducts.end(), sortId);

	cout << endl << "*** After Sorting ***" << endl;
	for (int i = 0; i < 9; i++)
		cout << "ID: " << MyProducts[i].ID << ", Name: " << MyProducts[i].name << ", Price: $" << MyProducts[i].price << endl;

	return 0;

}

oops, sorry MiiNiPaa I didn't refresh the post before posting hence a repeat post. Anyway, left it here as hes asked to sort by ID (not that there's much of a change though :p )

Must remember to hit that refresh in future :)
Topic archived. No new replies allowed.