Need help with insertion sort... think I'm close?

I am trying to sort my array in descending order by Price. The array is data type "Invoice". I have never used an insertion sort (yes I have to for my assignment) so any help to guide me towards a solution would be great. Is the structure of my sort correct? Also, I have one error as of right now.

Error:
Severity Code Description Project File Line Suppression State
Error C2659 '=': function as left operand U14 c:\users\christian\source\repos\u14\u14\u14.cpp 105

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void insertionSort(Invoice invoices[], const int NUM_ITEMS)
{
	int i, j;
	double key;
	for (i = 1; i < NUM_ITEMS; i++)
	{
		key = invoices[i].getPrice();
		j = i - 1;

		/* Move elements of arr[0..i-1], that are
		greater than key, to one position ahead
		of their current position */
		while (j >= 0 && invoices[j].getPrice() > key)
		{
			invoices[j + 1].getPrice = invoices[j].getPrice;
			j = j - 1;
		}
		invoices[j + 1].getPrice = key;
	}
}
I solved this on my own, but for anyone having this issue... I changed it up and this is what I came up with:

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
void insertion_sort(Invoice invoices[], const int NUM_ITEMS) 
{
	int j;
	Invoice temp;

	for (int i = 0; i < NUM_ITEMS; i++) 
	{
		j = i;

		while (j > 0 && invoices[j].getPrice() < invoices[j - 1].getPrice()) 
		{
			temp = invoices[j];
			invoices[j] = invoices[j - 1];
			invoices[j - 1] = temp;
			j--;
		}
	}
	cout << endl;


	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << invoices[i].getNum();
		cout << setw(11) << "\t" << invoices[i].getDes();
		cout << setw(11) << "\t" << invoices[i].getQuan();
		cout << setprecision(2) << fixed << setw(11) << "\t$" << invoices[i].getPrice() << endl;
	}
}
Topic archived. No new replies allowed.