Qt中 对QList的排序

Qt中的qSort可以对Qlist 进行排序

1、 数组

QList list;

list <<12  <<13<<16<<10;

qSort(list.begin(), list.end());

//排序结果:[10,12,13,16]

2、多维的数组

头文件:
class PosSortResult
{
public:
    int agvpos;
    int posid;
}
 //多条任务 需要依次排序
    QList posSortBeforeList;
;
static bool subDevListSort(const PosSortResult* info1, const PosSortResult* info2);
    void devListSort(QList list);
.cpp文件
{
PosSortResult *possort = new PosSortResult();
    possort->agvpos = 1;
    possort->posid = 1;
    posSortBeforeList.append(possort);
    PosSortResult *possortt = new PosSortResult();
    possortt->agvpos = 9;
    possortt->posid = 3;
    posSortBeforeList.append(possortt);
    PosSortResult *possorttt = new PosSortResult();
    possorttt->agvpos = 7;
    possorttt->posid = 2;
    posSortBeforeList.append(possorttt);
 
  
    devListSort(posSortBeforeList);
}
 
  
void MainWindow::devListSort(QList list)
{
    qSort(list.begin(), list.end(), subDevListSort);
    for(int i = 0; i 
    {
        qDebug()<< i << list.at(i)->posid<< list.at(i)->agvpos;   
    }
}
 
  
bool MainWindow::subDevListSort(const PosSortResult *info1, const PosSortResult *info2)
{
    return info1->posid < info2->posid;  //升序排列   
 //return info1->posid < info2->posid;     //降序排列  
}


你可能感兴趣的:(Qt的学习,QList,qSort)