#include <QtAlgorithms> #include <QList> #include <QDebug> /* bool compare(const double *d1,const double *d2) { return *d1<*d2; } int main() { QList<double *> list1; list1.append(new double(1.2)); list1.append(new double(2.2)); list1.append(new double(7.2)); list1.append(new double(3.2)); list1.append(new double(4.2)); qDebug() << list1; for(int i=0;i<list1.count();i++) { qDebug() << *(list1.at(i)); } qSort(list1.begin(),list1.end(),compare); qDebug() << list1; for(int i=0;i<list1.count();i++) { qDebug() << *(list1.at(i)); } return 0; } */ class Student { public: Student(int i,QString n,int g,int a) :id(i),name(n),grade(g),age(a){} int getId() const { return id; } QString getName() const { return name; } int getGrade() const { return grade; } bool operator<(const Student &s1) const { if(grade < s1.getGrade()) return true; if(grade == s1.getGrade()) { if(id < s1.getId()) return true; if(id == s1.getId()) { return name.toLower() < s1.getName().toLower(); }else { return false; } }else { return false; } } private: int id; QString name; int grade; int age; }; bool compare(const Student *s1,const Student *s2) { return *s1 < *s2; } int main() { QList<Student *> list; list.append(new Student(1,"jack",2,17)); list.append(new Student(5,"Luex",1,16)); list.append(new Student(9,"Henry",3,19)); list.append(new Student(10,"messi",4,22)); list.append(new Student(1,"henlen",2,17)); qDebug() << list; foreach(Student *s,list) { qDebug() << s->getId()<<" -- "<<s->getName()<<" -- "<<s->getGrade(); } qSort(list.begin(),list.end(),compare); qDebug() << list; foreach(Student *s,list) { qDebug() << s->getId()<<" -- "<<s->getName()<<" -- "<<s->getGrade(); } }