sort error: descending instead of ascending order

Hi,

I have made a small mistake in my code and cannot find what I did wrong.

The code is meant to display the second output in ascending order (1-10, a-z) instead it shoes it in descending order (z-a,10-1).

Could somebody tell me what I did wrong?

header file:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Product.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include <cmath>
#include <string>		  // header file used to manipulate and process strings and arrays
using namespace std;	  // instructs compiler to use names in standard library



	string sku;
	string product;
	string category;
	string unitPrice;
	string onHand;
	string ReorderLevel;
	string ReorderLeadTime;

	

	// temp variables used to input Product data from file
	

class Product
{
private:
	string SKU; // SKU
	string ProductName; // ProductName
	string category; // Category
	double unitPrice100; // UnitPrice
	int onHand; // OnHand
	int reorderLevel; // ReorderLevel
	int reorderLeadTime; // ReorderLeadTime

public:
	Product();
	Product(string sku, string product, string Category, double UnitPrice, int OnHand, int ReorderLevel, int ReorderLeadTime);

	void displayProductInfo();

	void setProductInfo(string sku, string product, string Category,
		double UnitPrice, int OnHand, int ReorderLevel,
		int ReorderLeadTime);

	string getProductName();		// for future alphabetic ordering

	bool operator < (Product & aProduct); 
	bool operator > (Product & aProduct);
};

/********************************* Implementation  *******************************/


Product::Product()
{
	// no arg Constructor
}

Product::Product(string sku, string product, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{
	SKU = sku;
	ProductName = product;
	category = Category;
	unitPrice100 = UnitPrice;
	onHand = OnHand;
	reorderLevel= ReorderLevel;
	reorderLeadTime = ReorderLeadTime;
}

void Product::setProductInfo(string sku, string product, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{
	SKU = sku;
	ProductName = product;
	category = Category;
	unitPrice100 = UnitPrice;
	onHand = OnHand;
	reorderLevel= ReorderLevel;
	reorderLeadTime = ReorderLeadTime;
}

void Product::displayProductInfo()
{
	cout << left << setw( 8 ) << SKU << setw( 30 ) << ProductName << setw( 11 ) << category << right << setw( 8 ) << onHand << setw( 11 ) << unitPrice100  << setw( 10 ) << fixed << setprecision( 2 ) << onHand*unitPrice100 << endl;
}

string Product::getProductName()
{
	return ProductName;
}

template<typename Tvar>
void sort(Tvar list[], int listSize)
{
	for (int i = 0; i < listSize; i++)
	{
		// Find the minimum in the list[i..listSize-1]
		Tvar currentMin = list[i];
		int currentMinIndex = i;

		for (int j = i + 1; j < listSize; j++)
		{
			if (currentMin < list[j])
			{
				currentMin = list[j];
				currentMinIndex = j;
			}
		}

		// Swap list[i] with list[currentMinIndex] if necessary
		if (currentMinIndex != i)
		{
			list[currentMinIndex] = list[i];
			list[i] = currentMin;
		}
	}
}

bool Product::operator < (Product & aProduct)
{
 return this-> ProductName < aProduct.ProductName;
}

bool Product::operator > (Product & aProduct)
{
 return this-> ProductName < aProduct.ProductName;
}



#endif 


cpp:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Inventory Report.cpp
//
// Purpose: Write a program that 
//          1. 
//          2. 
//          3. 
//          4. 
//
// Author: Anonymous
// Reference: http://www.cplusplus.com
// Date: 23 July 2011

#include <iostream>		  // header file for console I/O
#include <iomanip>		  // header file for stream manipulators
#include <string>		  // header file used to manipulate and process strings and arrays
#include <fstream>		  // header file used to read data from a file
#include "Product.h"	  // header file for class and function declarations
using namespace std;	  // instructs compiler to use names in standard library


/********************************* main() *******************************/
int main()
{
	// Display author name to the right, title of program, and dashes
	cout << right;
	cout << setw(60) << "Anonymous" << endl;
	cout << "Product Inventory Report - Sort by Product" << endl;
	cout << setfill ('-') << setw(42) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Declaration Section
	int arraySize=0;
	char *endp;

	Product inventory[10];


	// Open the file for input
	cout << "Products READ FROM FILE" << endl;
	cout << setfill ('-') << setw(23) << "-" << endl;
	cout << endl << endl;					// extra lines (for legability)

	ifstream inFile("inventory.csv");	// open file stream for input
	
	if (inFile.fail())
	{
		cout << "ERROR opening data file." << endl;
		return -1;
	}

	// read the contents of the file into an array of Product objects
	while (!inFile.eof())
	{
		// input Product record data from file
			getline(inFile, sku, ',');
			getline(inFile, product, ',');
			getline(inFile, category, ',');
			getline(inFile, unitPrice, ',');
			getline(inFile, onHand, ',');
			getline(inFile, ReorderLevel, ',');
			getline(inFile, ReorderLeadTime);
			
			inventory[arraySize].setProductInfo(sku, product, category,strtod(unitPrice.c_str(),&endp),
				atoi(onHand.c_str()), atoi(ReorderLevel.c_str()), atoi(ReorderLeadTime.c_str()));

			++arraySize;
		
	}

	inFile.close(); //close the file

	// Products array header
	cout << left << setfill (' ');
	cout << setw( 8 ) << "SKU #" << setw( 30 ) << "Product Name" << setw( 12 ) << "Category" << setw( 9 ) << "On Hand" << setw( 12 ) << "Unit Price" << setw( 10 ) << "Inv Value" << setw( 10 ) << endl;
	
	// Display ProductInfo
	for (int i=0; i < arraySize; i++)
		inventory[i].displayProductInfo();
		/*experiemntal > */
	cout << endl << endl << endl;	

	//cout<< product<<endl;
	/*< experimental*/
	cout << endl << endl << endl;			// extra lines (for legability)

	// Sort the Products alphabetically based on the Product Name
	cout << "Products SORTED BY Product Name" << endl;
	cout << setfill ('-') << setw(31) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Products array header
	cout << left << setfill (' ');
	cout << setw( 8 ) << "SKU #" << setw( 30 ) << "Product Name" << setw( 12 ) << "Category" << setw( 9 ) << "On Hand" << setw( 12 ) << "Unit Price" << setw( 10 ) << "Inv Value" << setw( 10 ) << endl;

	// Display sorted products

	/* Test Output Beginning */
			sort(inventory, arraySize);
			for (int j=0; j < arraySize; j++)
			inventory[j].displayProductInfo();
	/* Test Output End*/

		cout << endl << endl << endl;

	system("pause");
	return 0;
}



Any help is appreciated

Regards,

mark
Read these two lines closely.

1
2
3
			if (currentMin < list[j])
			{
				currentMin = list[j];


Also, not that you are using it, but your operator> is wrong.
Topic archived. No new replies allowed.