sorting structures

I am trying to sort a list of students by their ID numbers in ascending order. Along with their IDs are program and test averages. I have used a struct to hold all three of these items for each student. My program compiles, but it does not do the sorting. Does any one know what I'm missing?




void sortAr(persons[], int count)
{

int min,
i;

for (int i= 0; i < count; i++) //this loops checks for the smallest id value
{
int min = i;

for (int temp = i + 1; temp < count; temp++)
if (student[temp].studentID < student[min].studentID)
min = temp;



swap(student[i].studentID, student[min].studentID); //if a student ID is smaller than the past, the swap will
swap (student[i].programAvg, student[min].programAvg); //put them in ascending order
swap (student[i].testAvg, student[min].testAvg);

}

}


This is the function I wrote for the sorting.
Last edited on
try this syntaxis

for(i=0; i<count; i++)
{
for(j=i; j<count-1; j++)
{
if(student[j].studentID>student[j+1].studentID)
{
swap...
swap...
swap...
}
}
}
Last edited on
I tried it, the outcome looks the same.
closed account (DSLq5Di1)
Use the sort algorithm,

#include <algorithm>

1
2
3
4
bool student_id_asc(const persons& a, const persons& b) // comparison function
{
    return a.studentID < b.studentID;
}

1
2
std::sort(student.begin(), student.end(), student_id_asc);
std::sort(student, student + size_of_array, student_id_asc); // if you are not using an stl container 
Topic archived. No new replies allowed.