Dear all,

I have this code and this data...but i want to sort the sum1 in descending order..but my problem is..i want to sort many array....like this..

after sort
3: ............. 46
1: .............. 25
2:..................20


how to sort in descending order together with the row?


1: 5 6 7 8 29 48 49 68 69 70 73 74 75 76 95 96 103 104 109 110 111 130 132 134 137 25
2: 6 7 29 49 69 70 74 75 95 96 100 103 104 109 110 111 130 132 134 137 20 3: 9 10 11 12 13 14 15 16 30 31 32 33 34 35 36 37 50 51 52 53 54 55 56 57 71 77 78 79 80 81 82 83 84 97 105 106 112 113 114 115 116 117 118 119 135 138 46

#include <iostream>  
#include <fstream>
#include <iomanip>
#include <string>   
#include <vector>    
#include <algorithm> 
using namespace std; 

int g[2000],g1[2000],h[200], s[200];
struct student
{
string studentid;
vector <int> examcode;   
}; 

int myfile;
int main()
{
ifstream stream1 ("STA83STU.txt");
if ( !stream1 )

	{
	cout << "While opening a file an error is encountered" << endl;
	}
	
	else
	{
	cout << "File is  opened" << endl;
	} 		
	vector <student> students;
	student aStudent;
	string tempStudentID;
	bool readEntireFile = false;     
    stream1 >> tempStudentID;   
	while ( !readEntireFile )
{
aStudent.studentid = tempStudentID;  
int tempExamCode;
aStudent.examcode.clear ();
stream1 >> tempExamCode;    
aStudent.examcode.push_back (tempExamCode);  
bool newStudent = false;  
while ( !newStudent && !readEntireFile )
	
	{
	if ( stream1 >> tempStudentID )   
		{
		if ( tempStudentID.compare (aStudent.studentid) == 0 )  
					
			{
			stream1 >> tempExamCode;   
			aStudent.examcode.push_back (tempExamCode); 
			}
				
		else
		newStudent = true;  
		}
				
	else
	readEntireFile = true;  
	}  
	students.push_back (aStudent);   
}
stream1.close ();  
				
{
ofstream myfile;
myfile.open ("212-1.txt");
			
	if (myfile.is_open())
	{
		{
      		{ 
			for (int colHead=1; colHead<140; colHead++)
			cout<<endl;
			int sum, temp,sum1, temp1;
			int count,count2, count3;
			int m, n;
		
			temp=temp1=0;	
    		
				for ( m=1; m<5; m++)
				{  	myfile<<setw(3)<<"\n"<<m<<":";
					for (n=1; n<140; n++)
					{
					count=h[n]=count2=count3=0;
					g1[n]=n;
						for ( int i = 1 ; i < students.size (); i++ )
						{
							for ( int k = 1; k<students.at(i).examcode.size(); k++ )
							{	
								if (students.at(i).examcode.at(0)==m && students.at (i).examcode.at(k)==n)
								{
								count++;
								}
									
								if (students.at(i).examcode.at(0)==n && students.at (i).examcode.at(k)==m)
								{
								count++;
								}	
							}
						}
					g[i]=count;
					sum=g[i]+temp;
					temp=sum;
				
					if(g[i]>=1)
					{
					count2++;
					h[n]=count2;
					}		
					sum1=h[n]+temp1;
					temp1=sum1;

					if(g[i]>=1 )
					{
					myfile<<"\t"<<g1[n];
					}
				  
					}
				
					myfile<<"\t"<<sum1; 
					sum=temp=sum1=temp1=0;
				

					}	
				
			
				
			}	
		}		
	}
return 0;
	}

}

Dear all,

I have this code and this data...but i want to sort the sum1 in descending order..but my problem is..i want to sort many array....like this..

after sort
3: ............. 46
1: .............. 25
2:..................20


how to sort in descending order together with the row?


1: 5 6 7 8 29 48 49 68 69 70 73 74 75 76 95 96 103 104 109 110 111 130 132 134 137 25
2: 6 7 29 49 69 70 74 75 95 96 100 103 104 109 110 111 130 132 134 137 20 3: 9 10 11 12 13 14 15 16 30 31 32 33 34 35 36 37 50 51 52 53 54 55 56 57 71 77 78 79 80 81 82 83 84 97 105 106 112 113 114 115 116 117 118 119 135 138 46

The numbers in red are the number of elements in each row. nurulshidanoni, this is something you need to point out. Don't expect people to understand that that is why you highlighted it, particularly since you didn't start row 3 with a new line. I assume each "row" is one student's exam codes? I have no idea what "sum1" refers to. So I'm guessing your question is "How do I sort a vector of integers in descending order?" There are lots of sorts out there. The easiest, in my opinion, is the Bubble Sort:
http://en.wikipedia.org/wiki/Bubble_sort

It then appears that you want to sort entire rows by the number of elements in them in descending order. Bubble sort again, except this time instead of swapping two integers, you are swapping entire rows/vectors.

Thank you vernon dozier for your great suggestion....

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.