Solved qSort a QList<struct> by QTime
-
Scene: a line of lyric contains more than one time tag.
struct Lyric{ QTime time; QString sentence; }; QList<Lyric> lyrics; ...... lyrics.clear(); QStringList line = slyric.split("\n"); for(int i=0; i<line.size(); i++){ if(line.at(i).contains("]")){ QStringList SL = line.at(i).split("]"); for(int j=0; j<SL.length()-1; j++){ Lyric lyric; QString stime = SL.at(j).mid(1); if((stime.length() - stime.indexOf(".")) == 3) stime += "0"; lyric.time = QTime::fromString(stime, "mm:ss.zzz"); lyric.sentence = SL.at(SL.length()-1); lyrics.append(lyric); } } } for(int i=0; i<lyrics.size(); i++){ textBrowser->insertPlainText(lyrics.at(i).time.toString("mm:ss.zzz") + " " + lyrics.at(i).sentence + "\n"); }
I need something like:qSort(lyrics.begin(), lyrics.end(), Lyric.time);
-
@sonichy said in qSort a QList<struct> by QTime:
qSort
U mean this http://doc.qt.io/archives/qt-4.8/qtalgorithms.html#qSort-2 ?
-
you can use
std::sort
and define your own function /lambda to sort by.something like this (untested)
QList<Lyric> lyrics; std::sort(lyrics.begin(), lyrics.end(), [](const Lyric& a, const Lyric& b)->bool{return a.time < b.time;});
-
@J.Hilk You are really something!