Array of Struct Sorting

I have read many topics regarding sort but i wasn`t able to sort them. I am new in C++. What changes i must do to sort the names alphabetically ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class classroom
{
public:
    string studentName;

    void classroom::createStudent(string);
};

void classroom::createStudent(string name)
{
    studentName=name;
}

int main()
{
    //number of students
    const int numstudents=10;
    
    //create structure
    classroom student[numstudents];

    //initialize variables
    student[0].createStudent("Alexa Trina");
    student[1].createStudent("George Ali");
    student[2].createStudent("Comina Riviera");
    student[3].createStudent("Dimitri Askin");
    student[4].createStudent("Erato Georgin");
    student[5].createStudent("Georgina Mare");
    student[6].createStudent("Konan Varvi");
    student[7].createStudent("Lampros Anesti");
    student[8].createStudent("Marina Parate");
    student[9].createStudent("Nikon Smith");

    return 0;//indicate that program end succesfully
}//end main 


Thanks for you time!
Last edited on
I figure out how to do it but with a simple problem.
I add to the previous post`s code the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     bool doMore;

    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<10-1; i++) {
            if (student[i].studentName > student[i+1].studentName) {
                // exchange elements
                classroom temp = student[i]; student[i] = student[i+1]; student[i+1] = temp;
                doMore = true;  // after exchange, must look again
            }
        }
    } while (doMore);

        for (int j=0;j<10;j++)
        {
        cout<<student[j].studentName;
        cout<<endl;
        }


but i got
Comina RivierbAlexa Trina
Comina Riviera
Comina Rivierb
Dimitri Askin
Erato Georgin
Georgina Mare
Konan Varvi
Lampros Anesti
Marina Parate
Nikon Smith


The first Comina Rivierb shouldn`t be there. What is the mistake in my code ?
I just had to my code one more cout before the loop :/
Topic solved!
Topic archived. No new replies allowed.