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]);
	}
}

Here's an example based on your code that may help:

void sortData(int lId[], int fId[], int noOfRows);

int main ()
{
   int lId[4];
   int fId[4];
   while (inFile >> fId[i])
   {
      inFile >> lId[i] ;
      i++;
    }
    sortData(lId, fId, i);

    for (i = 0; i < 4; i++)
    {
       outFile <<  " " << fId[i] <<"  "<< lID[i] << endl;
      }
}


void sortData(int lId[], int fId[], int noOfRows)
{
    int i, j;
    int temp;

    //bubble sort
    for (i = 0; i < noOfRows - 1; i++)
    {
       for (j = i + 1; j < noOfRows; j++)
       {
          if (lId[i] < lId[j])
          {
              temp = lId[j];
              lId[j] = lId[i];
              lId[i] = temp;

              temp = fId[j];
              fId[j] = fId[i];
              fId[i] = temp;
          }
     }	
}

but I want to do a selection sort because it more quickly than bubble sort

Then use the principles demonstrated and and convert the example into a selection sort. But for 4 items to sort, you won't notice a difference between the two types of sorts.

I try to read this file firt , but it have -858993460 in the last?

1 20
2 15
3 50
-858993460
Press any key to continue

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

void sortData( string fId[], int noOfRows);

int main ()
{
	// step 1

	string fId[4];
	int total[4],  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]  >> total[i] ;
		i++;
	}
	sortData( fId, i);

	for (i = 0; i < 4; i++)
	{
		cout<<  " " << fId[i] <<"  "<<total[i] <<endl;
	}
	
	return 0;
}


void sortData(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 (fId[j] < fId[max])
				max= j;

		if(max!=i)// step b
		fId[i].swap(fId[max]);
	}
}

I try to read this file firt , but it have -858993460 in the last?

1 20
2 15
3 50
-858993460
Press any key to continue

When are you going to start asking questions in a way that we don't have to guess what the problem is? Either change the input file to match the program or change the program to match the input file. What does -858993460 represent?

This is a data that I wnt try to read..but why when i compile it appear -858993460
after 50?why..i dont know what is -858993460

1 20
2 15
3 50

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

void sortData( string fId[], int noOfRows);

int main ()
{
	string fId[4];
	int total[4],  i = 0, num = 0;

	ifstream inFile; 
	ofstream outFile; 

	inFile.open("jes.txt");

	if (!inFile) 
	{
		cout << "Cannot open the input file." << endl;
		return 1;
	}

	outFile.open("jes2.txt");

	while (!inFile.eof())
	{
		inFile >> fId[i]  >> total[i] ;
		i++;
	}

	for (i = 0; i < 4; i++)
	{
		cout<<" "<<fId[i]<<"  "<<total[i] <<endl;
	}
	
	return 0;
}

for (i = 0; i < 3; i++)

yehaaaaaaaaaaa...i get that it..after i change from 4 to 3 then the -858993460
dont appear......

This is a data that I wnt try to read..but why when i compile it appear -858993460
after 50?why..i dont know what is -858993460

1 20
2 15
3 50

Well, is -858993460 in the input file or is it in the output when your run it or both? My point is that we have no way of knowing from posts like this:

I try to read this file firt , but it have -858993460 in the last?

1 20
2 15
3 50
-858993460
Press any key to continue

You need to start being a lot more clear in your posts so we don't have to continually ask you for clarification.

-858993460
is the output file,,,,thank you Master Vernon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.