Easiest way to put these totals into ascending order?

I need to put these totals in ascending order. What is the easiest way to do so? Do I have to write an entire selection sort? *WITHOUT USING BUILT IN SORT*
1
2
3
4
5
6
7
8
9
10
11
12
13
void getCost(Invoice *invoices, const int NUM_ITEMS)
{
	cout << endl;
	cout << setw(7) << "Display Description and totals\n";
	cout << "----------------------------------------------------------------\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		double cost = invoices[i].getPrice() * invoices[i].getQuan();
		cout << invoices[i].getDes();
		cout << setprecision(2) << fixed << setw(12) << "\t$" << cost << endl;
	}
}
Last edited on
You can use the sort function from the C++ library.
I don't know the exact structure of your Invoice, but the following may give you the idea.
Reverse the test (< to >) if you want to sort descending.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>
using namespace std;

class Invoice {
    int n;
public:
    Invoice(int n) : n(n) {}
    int getn() const { return n; }
};

int main() {
    Invoice invoices[] = {6,3,4,9,7,0,2,5};
    int NUM_ITEMS = 8;

    cout << "Before: ";
    for (const auto& inv: invoices) cout << inv.getn() << ' '; cout << "\n\n";

    sort(&invoices[0], &invoices[NUM_ITEMS],
         [](const Invoice& x, const Invoice& y){return x.getn() < y.getn();});

    cout << "After : ";
    for (const auto& inv: invoices) cout << inv.getn() << ' '; cout << '\n';
}

Last edited on
What would be the best way without using the built in sort?
Then you have to write your own. Selection sort is good if what you need to sort is not that large. If you are trying to sort a million records, then an N2 sort is not good enough so you need to go to something like quicksort.
Last edited on
@stormbot, Why "without using the built-in sort" ? You realize that for containers filled with custom objects, like your Invoice, the std::sort is perfectly suited to accept a custom comparator as its third parameter, which you can write yourself. You can have it as a lambda, as demonstrated by tpb, or as a functor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct InvoiceSorter 
{
    bool operator()(const Invoice& a, const Invoice& b) 
    {
        return a.getn() < b.getn();
    }
};

int main()
{
    //...
    sort(invoices, invoices + NUM_ITEMS, InvoiceSorter());
    //...
}


As you can see, this sorts an array of Invoice objects with a custom comparator, which should demonstrate to your teacher that you understand the concept.

Btw, do you actually want to sort the results of your calculations (your costs) instead? You could make another array of size NUM_ITEMS filled with the cost doubles, sort ing + printing that. Just think carefully about how you want it all to look in the end -- you don't want to be printing sorted costs next to unsorted preliminary values.
Last edited on
fuller example based on some of your methods:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

class Invoice 
{
public:
    Invoice(string description, double price, int quantity) :
        description_(description),
        price_(price),
        quantity_(quantity),
        cost_(price * quantity)
    {
    }

    const string& Description() { return description_; }
    double Price() { return price_; }
    int Quantity() { return quantity_; }
    double Cost() const { return cost_; }

private:
    string description_;
    double price_;
    int quantity_;
    double cost_;  // Total cost
};

struct InvoiceSorter 
{
    bool operator()(const Invoice& a, const Invoice& b) const
    {
        return a.Cost() < b.Cost();
    }
};

void Show(Invoice* arr, int arr_size)
{
    for(int i=0; i<arr_size; ++i)
    {
        Invoice& v = arr[i];
        cout <<  right << setw(15) << v.Description() << 
                right << setw(7) << v.Price() << " * " <<
                left << setw(5) << v.Quantity() << "  $" <<
                left << setw(7) << v.Cost() << endl;
    }
    cout << endl;
    
}

int main() 
{
    int NUM_ITEMS = 8;
    Invoice invoices[] = 
    {
        { "Bat", 2.95, 3 },
        { "Baseball", 5.99, 4},
        { "Glove", 2.56, 1},
        { "Hockey Stick", 10.24, 2},
        { "Mask", 4.24, 2},
        { "Hockey Puck", 3.67, 1},
        { "Tennis Racket", 16.42, 2},
        { "Tennis Ball", 0.99, 9}
    };

    cout << fixed << setprecision(2);
    cout << "Before: " << endl;
    Show(invoices, NUM_ITEMS);

    cout << "After: " << endl;
    sort(invoices, invoices + NUM_ITEMS, InvoiceSorter());
    Show(invoices, NUM_ITEMS);
    
    return 0;
}


Before: 
            Bat   2.95 * 3      $8.85   
       Baseball   5.99 * 4      $23.96  
          Glove   2.56 * 1      $2.56   
   Hockey Stick  10.20 * 2      $20.40  
           Mask   4.24 * 2      $8.48   
    Hockey Puck   3.67 * 1      $3.67   
  Tennis Racket  16.42 * 2      $32.84  
    Tennis Ball   0.99 * 9      $8.91   

After: 
          Glove   2.56 * 1      $2.56   
    Hockey Puck   3.67 * 1      $3.67   
           Mask   4.24 * 2      $8.48   
            Bat   2.95 * 3      $8.85   
    Tennis Ball   0.99 * 9      $8.91   
   Hockey Stick  10.20 * 2      $20.40  
       Baseball   5.99 * 4      $23.96  
  Tennis Racket  16.42 * 2      $32.84 
Last edited on
Topic archived. No new replies allowed.