Problem of sorting ascending order by according to PRODUCT ID,NAME,PRICE

Here is my code below:
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<fstream>
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<string>
using namespace std;
void main()	
{
	fstream file;
	string id,name,type,price;
	file.open("test.txt", ios::in);
    if(!file.is_open())
		cout << "Can't open file!\n";
	else{
    cout << left << setw(14) << "Product ID" << setw(30) << "Name" << setw(15) << "Type" << setw(11) << "Price  (HK$)\n";
	cout << "----------------------------------------------------------------------\n";
	while(file >> id >> name >> type >> price)
			cout << setw(14) << setiosflags(ios::left) << id
					<< setw(30) << setiosflags(ios::left) << name 
					<< setw(15) << setiosflags(ios::left) << type
					<< setw(11) << setiosflags(ios::left) << price << endl;
		
}
}

I can't separate and align 4 categories neatly, how to do this solution below(the link of example for sorting Product ID in ascending order):
http://postimg.org/image/twytfdxnn/
By the way,here is the required .txt file(test.txt):
http://m.uploadedit.com/b037/1405837527237.txt
Last edited on
You cannot sort, if you know only one value at a time. You have to store all the values first, and then sort.

Your values are records that each contain four fields. You should use a struct to store one record. Then you can provide functions that take two records and return true if they are in "correct order". The logic of the function dictates which field is most important.

See std::sort.
Sorry, could you give some source code to sort the correct order,please?
The first thing to do is to read the data from the file in the required format. The file appears to use the tab character to separate the fields, so you can use getline() with '\t' as the delimiter, apart from the final field on each line, where the delimiter will be '\n'.
http://www.cplusplus.com/reference/string/string/getline/
Topic archived. No new replies allowed.