I have this data, but when i sort it different from what i want.
I want to sort in descending order like this
2 30
1 20
3 15
jest.txt
1 20
2 30
3 15
after compile
1 15
30 15
15
15
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
void sortData(string lId[], string fId[], int noOfRows);
int main ()
{
// step 1
string lId[4];
string fId[4];
int total, i = 0, num = 0;
ifstream inFile; // input stream variable for data file
ofstream outFile; // output stream variable for result data
inFile.open("jes.txt");
if (!inFile) // step 3
{
cout << "Cannot open the input file." << endl;
return 1;
}
outFile.open("jes2.txt");// step 4
while (!inFile.eof())
{
inFile >> fId[i] >> lId[i] >> total ;
i++;
}
sortData(lId, fId, i);
for (i = 0; i < 4; i++)
{
outFile << " " << fId[i] <<" "<<total <<endl;
}
return 0;
}
void sortData(string lId[], string fId[], int noOfRows)
{
int i, j;
int max;
// selection sort
for (i = 0; i < 4; i++)
{
// step a
max = i;
for (j = i ; j < noOfRows; j++)
if (lId[j] < lId[max])
max= j;
if(max!=i)// step b
lId[i].swap(lId[max]);
}
}