2

I have a QVector, which should store filepaths, like

"./zThumbnail/1004.jpeg"
"./zThumbnail/1005.jpeg"
"./zThumbnail/1006.jpeg"
"./zThumbnail/1007.jpeg"
"./zThumbnail/1008.jpeg"
"./zThumbnail/1009.jpeg"
"./zThumbnail/101.jpeg"
"./zThumbnail/1010.jpeg"
"./zThumbnail/1011.jpeg"
"./zThumbnail/1012.jpeg"
"./zThumbnail/1013.jpeg"

QFileInfoList filelistinfo = dir.entryInfoList();
foreach (const QFileInfo &fileinfo, filelistinfo) {
imagePath.push_back(fileinfo.absoluteFilePath());
qDebug() << fileinfo.absoluteFilePath();
}


./abc/1.jpg
./abc/2.jpg
./abc/3.jpg
.
.
.
./abc/1209.jpg

But instead of storing it in the above order, it's storing like

./abc/1.jpg
./abc/10.jpg
./abc/100.jpg
./abc/1000.jpg
./abc/1001.jpg
.
.

How can I store the image filepath in numeric sequence?

1 Answer 1

5

You can sort using std::sort, for that matter I'm assuming that the base name of the file is an integer.

QFileInfoList filelistinfo = dir.entryInfoList();
for(const QFileInfo &fileinfo: filelistinfo) 
    imagePath.push_back(fileinfo.absoluteFilePath());

std::sort(imagePath.begin(), imagePath.end(),
          [](const QString & a, const QString & b) -> bool
{
    return QFileInfo(a).baseName().toInt() < QFileInfo(b).baseName().toInt();
});

qDebug()<<imagePath;

Note: using qSort is not recommended currently since it is generally slower than std::sort, for more information read: Performance of qsort vs std::sort?

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.