What I aim is that I would like to sort the vector by following criteria; I sorted the whole vector by "name" and when "name"s are equal, I want it to sort by "surname" etc. Here is the following code:
Below I created a class;
class NotLearning {
public:
NotLearning(); // Constructor
int getID()const; // Function to used to initialise private variable "ID" of the class via the constructor.
int getDay()const; // Function to used to initialise private variable "day" of the class via the constructor.
int getYear()const; // Function to used to initialise private variable "year" of the class via the constructor.
double getAverage()const; // Function to used to initialise private variable "average" of the class via the constructor.
std::string getName()const; // Function to used to initialise private variable "name" of the class via the constructor.
std::string getSurname()const; // Function to used to initialise private variable "surname" of the class via the constructor.
std::string getCity()const; // Function to used to initialise private variable "city" of the class via the constructor.
std::string getMonth()const; // Function to used to initialise private variable "month" of the class via the constructor.
private:
int ID;
int day;
int year;
double average;
std::string name;
std::string surname;
std::string city;
std::string month;
};
I sort the vector, that consists of class objects, by the class member of "ID" with the following code:
void sortVectorByID(std::vector<NotLearning>& newClass)
{
std::sort(newClass.begin(), newClass.end(), cmp_by_id);
}
In which "cmp_by_id" define like that;
bool cmp_by_id(const NotLearning& one, const NotLearning& two) // compare classes inside the vector by checking the member of "ID".
{
return one.getID() < two.getID();
}
So far I have sorted the vector successfully. After that, I implemented the following code for my purpose;
void sortVectorByDetail(std::vector<NotLearning>& newClass)
{
std::sort(newClass.begin(), newClass.end(), cmp_by_detail);
}
Where "cmp_by_detail" defined like that;
bool cmp_by_detail(const NotLearning& one, const NotLearning& two) // compare classes inside the vector by checking the members in order.
{
bool compare_result = cmp_by_id(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_average(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_name(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_surname(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_city(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_day(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_month(one, two);
if (compare_result)
return compare_result;
compare_result = cmp_by_year(one, two);
if (compare_result)
return compare_result;
return true;
}
I did not give all codes in the class, just in order not to distract you. However, I leave GitHub here if you wanted to see all codes.
I hope I could tell you what I need.
I thank you deeply for sparing your time if you came to this line ^^.
Have a nice day.
Edit: I forgot to mention that I get an error that says debug assertion failed invalid comparator when I called sortVectorByDetail() function. I also below leave the way I used to call these functions;
#include "LearningCpp.h"
int main()
{
srand((unsigned int)time(NULL));
std::vector<NotLearning> myClass;
fillVector(myClass);
showVector(myClass);
sortVectorByDetail(myClass);
showVector(myClass);
}
Edit: I now solved my problem with following implementation to the function of cmp_by_detail. Code is here;
bool cmp_by_detail(const NotLearning& one, const NotLearning& two) {
if (one.getID() != two.getID())
{
return one.getID() < two.getID();
}
if (one.getAverage() != two.getAverage())
{
return one.getAverage() < two.getAverage();
}
if (one.getName() != two.getName())
{
return one.getName() < two.getName();
}
if (one.getSurname() != two.getSurname())
{
return one.getSurname() < two.getSurname();
}
if (one.getCity() != two.getCity())
{
return one.getCity() < two.getCity();
}
if (one.getDay() != two.getDay())
{
return one.getDay() < two.getDay();
}
if (one.getMonth() != two.getMonth())
{
return one.getMonth() < two.getMonth();
}
if (one.getYear() != two.getYear())
{
return one.getYear() < two.getYear();
}
return true;
}