# Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty simple. We have t sort the details of five students using the idea of the structure.

Below is the structure template that we are going to use before we commence with coding:

``````struct Student .
{
string name; //it will be a given detail
int mathematics;  //the Marks in math (Given)
int physics;   //the Marks in Physics (Given)
int chemistry;   //the Marks in Chemistry (Given)
int total marks; //the Total marks (To be filled)
int rank after sorting ;  //display Rank of student (To be filled)
};
``````

To understand the long lines of code, we will start by understanding different concepts of the C++ programming language we have used to build the logic in our program to display the details of the five student use cases in this scenario.

We have std::sort() function concept in C++ programming language. In this structure sorting, structure object qualities or structure object properties that we have given to them will all be made applicable to objects sorted based on the property of that object.

C++ code to demonstrate Structure Sorting (By multiple Rules) in C++

Code

``````//Here we are writing the code in the
//C++ program to demonstrate Structure Sorting in C++
#include <bits/stdc++.h> //initializing the library
using namespace std; //utilizing the namespace std

struct Student_details
{
string name; //it is a given detail
int mathematics; //the marks in mathematics also a (Given) detail
int physics; //the marks in physics also a (Given) detail
int chemistry; //the marks in chemistry also a (Given) detail
int total; //the marks in the total also a (Given) detail
int rank; //the marks in tital calculated for the rank a caluclted.
};

//down the codes are written for hunction for comparing two students //according to given rules
// function starts here
bool compare_Two_Students(Student_details A, Student_details B)
{
// If total marks are not same then
// returns true for higher total
if (A.total != B.total)
return A.total > B.total;

// If marks in Maths are same then
// returns true for higher marks
if (A.mathematics != B.mathematics)
return A.mathematics > B.mathematics;

if (A.physics != B.physics)
return A.physics > B.physics;

return (A.chemistry > B.chemistry);
}

// it will Fill total marks and ranks of all Students
void compute_Ranks(Student_details A[], int n)
{
// from here To calculate total marks for all Students
for (int i = 0; i < n; i++)
A[i].total = A[i].mathematics + A[i].physics + A[i].chemistry;

//this is the Sort structure array using user defined
//this is the function compareTwoStudents()
sort(A, A + 5, compare_Two_Students);

//from here Assigning ranks after sorting
for (int i = 0; i < n; i++)
A[i].rank = i + 1;
}

// this is the Driver code
int main()
{
int n = 5;

//here we have an array of structure objects
Student_details A[n];

// display details of Student 1
A.name = "helo";
A.mathematics = 180;
A.physics = 195;
A.chemistry = 385;

//display details of Student 2
A.name = "Peter";
A.mathematics = 395;
A.physics = 825;
A.chemistry = 299;

//display details of Student 3
A.name = "Galra";
A.mathematics = 395;
A.physics = 285;
A.chemistry = 380;

//display details of Student 4
A.name = "Jobs";
A.mathematics = 280;
A.physics = 270;
A.chemistry = 390;

//display details of Student 5
A.name = "Joshi";
A.mathematics = 380;
A.physics = 380;
A.chemistry = 380;

compute_Ranks(A, n);

//display column names for displaying data
cout << "Rank"
<< " "
<< "Name"
<< "     ";
cout << "Maths"
<< " "
<< "Physics"
<< " "
<< "Chemistry";
cout << " "
<< "Total\n";
// Display details of Students from here
for (int i = 0; i < n; i++) {
cout << A[i].rank << "    ";
cout << A[i].name << "      ";
cout << A[i].mathematics << "     " << A[i].physics << "     "
<< A[i].chemistry << "       ";
cout << A[i].total << " ";
cout << "\n"; }
return 0;}
``````

Output

``````Rank  Name      Maths  Physics Chemistry  Total
1    Peter      395     825     299       1519
2    Joshi      380     380     380       1140
3    Galra      395     285     380       1060
4    Jobs       280     270     390       940
5    helo       180     195     385       760
``````