Important: Please read the Qt Code of Conduct -

Reverse iterate on QMultiMap

  • Hello, maybe its a very treated theme, but in moment I coud not find a satisfactory answer in google.

    I have the next QMultiMap List:

        QMultiMap<QString, QString> data;
        data.insert("John", "Young");
        data.insert("John", "Tall");
        data.insert("Mike", "Short");
        data.insert("Mike", "Good man");
        data.insert("John", "Clueless");
        data.insert("Mike", "Long hair");
        for(const QString &name : data.uniqueKeys()){
            QStringList aux = data.values(nombre);
            qDebug() << "Name: " << name;
            for(const QString &aux2 :  aux) {
                qDebug() << aux2;

    The code produces the next output

    Name:  "John"
    Name:  "Mike"
    "Long hair"
    "Good man"

    The list is in The list is in random order displayed.

    Is there a way to show the elements in reverse order? .... Or in a determined order, for example, alphabetical?

    Thanks in avance.

  • @Josz
    I assume you mean the "multi-items" associated with each key in reverse order. Then don't use for(const QString &aux2 : aux): use a reverse iterator, or count down from aux.length() - 1 yourself, or reverse the QStringList.

  • @JonB
    thank you john! I will take a look

  • QMap iterators are bidirectional: for(auto i = map.keyEnd()-1;i!=map.keyBegin();--i)

Log in to reply