Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qSort a QList<struct> by QTime



  • Scene: a line of lyric contains more than one time tag.

    alt text

    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");
    }
    

    alt text
    I need something like:

    qSort(lyrics.begin(), lyrics.end(), Lyric.time);
    



  • Moderators

    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!


Log in to reply