vendredi 6 mars 2020

How to sort a vector of a struct based on two of the struct fileds [duplicate]

I have seen the similar question for one field but I am stuck trying to do this for two fields. In my program I want to write a sort function for a variable of type CourseTable which is a vector of the type Course.The courses in the table should be ordered decreasingly based on their number of units and if they have the same number of units , they should be ordered based on their names(which are strings) increasingly.I have done this using different ways (writing my own sort function etc) but I have trouble especially in handling the data types(swapping all the fields of the struct) .

    struct Course ;
     struct Session ;
     typedef vector<int> PreIds ;
     typedef vector<Session> CourseTime;
     typedef vector<Course> CourseTable;


    struct Course
    {
      int id;
      string name;
      int units;
      CourseTime schedule;
      PreIds prerequisites;
    };

    struct Session
    {
      string day;
      float start;
      float end;
    };

Imagine I have a given variable CourseTable takable_courses and I want to sort it in the manner I explained.

Aucun commentaire:

Enregistrer un commentaire