qlist 对结构体排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_33859977/article/details/80526831

写法一


    QList<test> s;
    test aa;
    test bb;
    test cc;
    aa.num = "14";
    bb.num = "2";
    cc.num = "3";
    s.append(aa);
    s.append(bb);
    s.append(cc);

    qSort(s.begin(), s.end(),[](const test &infoA,const test &infoB){return infoA.num.toDouble() < infoB.num.toDouble();});

    for(int i = 0; i < s.count() ; i++)
    {
        qDebug() << s.at(i).num;
    }

写法二

#include "widget.h"
#include <QApplication>
#include <QtDebug>

//排列判断
int compare(const test &infoA,const test &infoB)
{
    return infoA.num.toDouble() < infoB.num.toDouble();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QList<test> s;
    test aa;
    test bb;
    test cc;
    aa.num = "14";
    bb.num = "2";
    cc.num = "3";
    s.append(aa);
    s.append(bb);
    s.append(cc);

    qSort(s.begin(), s.end(),compare);

    for(int i = 0; i < s.count() ; i++)
    {
        qDebug() << s.at(i).num;
    }

    return a.exec();
}
展开阅读全文

没有更多推荐了,返回首页