Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString::localeAwareCompare() incorrect for å, ä and ö in Swedish
Forum Updated to NodeBB v4.3 + New Features

QString::localeAwareCompare() incorrect for å, ä and ö in Swedish

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 110 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote last edited by AndyBrice
    #1

    If I run this code:

    #include <QCoreApplication>
    #include <QLocale>
    
    int main( int argc, char* argv[] )
    {
        QCoreApplication app( argc, argv );
    
        QLocale::setDefault( QLocale( QLocale::Swedish ) );
    
        QString s1 = "a";
        QString s2 = "b";
        QString s3 = "o";
        QString s4 = "z";
        QString s5 = "å";
        QString s6 = "ä";
        QString s7 = "ö";
    
        qDebug() << "QString::localeAwareCompare( s1, s2 )=" << QString::localeAwareCompare( s1, s2 );
        qDebug() << "QString::localeAwareCompare( s2, s3 )=" << QString::localeAwareCompare( s2, s3 );
        qDebug() << "QString::localeAwareCompare( s3, s4 )=" << QString::localeAwareCompare( s3, s4 );
        qDebug() << "QString::localeAwareCompare( s4, s5 )=" << QString::localeAwareCompare( s4, s5 );
        qDebug() << "QString::localeAwareCompare( s5, s6 )=" << QString::localeAwareCompare( s5, s6 );
        qDebug() << "QString::localeAwareCompare( s6, s7 )=" << QString::localeAwareCompare( s6, s7 );
    
        return app.exec();
    }
    

    It returns:

    QString::localeAwareCompare( s1, s2 )= -1
    QString::localeAwareCompare( s2, s3 )= -1
    QString::localeAwareCompare( s3, s4 )= -1
    QString::localeAwareCompare( s4, s5 )= 1
    QString::localeAwareCompare( s5, s6 )= 1
    QString::localeAwareCompare( s6, s7 )= -1
    

    The locale aware sort order in Swedish is: a-z, å, ä, ö. So the results should all be -1.

    I am on Windows 11 and I get the same result to Qt 5.15.19 and Qt 6.7.3.

    Is this a bug, or am I doing something wrong?

    It seems the same issue was also in Qt 4, but was fixed: https://stackoverflow.com/questions/3773914/why-isnt-qstringlocaleawarecompare-working-correctly

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      Can you check with a more recent version of Qt ? I remember work being done on that field recently.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote last edited by
        #3

        @AndyBrice I'd print the contents of s1 to s7 just to make sure they really contain what you expect them to contain...

        Qt has to stay free or it will die.

        1 Reply Last reply
        0
        • AndyBriceA Offline
          AndyBriceA Offline
          AndyBrice
          wrote last edited by
          #4

          Did it again in Qt 6.10.0 on Windows 11:

          #include <QCoreApplication>
          #include <QLocale>
          
          int main( int argc, char* argv[] )
          {
              QCoreApplication app( argc, argv );
          
              QLocale::setDefault( QLocale( QLocale::Swedish ) );
          
              QString s1 = "a";
              QString s2 = "b";
              QString s3 = "o";
              QString s4 = "z";
              QString s5 = "å";
              QString s6 = "ä";
              QString s7 = "ö";
          
              qDebug() << "s1=" << s1;
              qDebug() << "s2=" << s3;
              qDebug() << "s3=" << s3;
              qDebug() << "s4=" << s4;
              qDebug() << "s5=" << s5;
              qDebug() << "s6=" << s6;
              qDebug() << "s7=" << s7;
          
              qDebug() << "QString::localeAwareCompare( s1, s2 )=" << QString::localeAwareCompare( s1, s2 );
              qDebug() << "QString::localeAwareCompare( s2, s3 )=" << QString::localeAwareCompare( s2, s3 );
              qDebug() << "QString::localeAwareCompare( s3, s4 )=" << QString::localeAwareCompare( s3, s4 );
              qDebug() << "QString::localeAwareCompare( s4, s5 )=" << QString::localeAwareCompare( s4, s5 );
              qDebug() << "QString::localeAwareCompare( s5, s6 )=" << QString::localeAwareCompare( s5, s6 );
              qDebug() << "QString::localeAwareCompare( s6, s7 )=" << QString::localeAwareCompare( s6, s7 );
          
              return app.exec();
          }
          

          Still wrong:

          s1= "a"
          s2= "o"
          s3= "o"
          s4= "z"
          s5= "å"
          s6= "ä"
          s7= "ö"
          QString::localeAwareCompare( s1, s2 )= -1
          QString::localeAwareCompare( s2, s3 )= -1
          QString::localeAwareCompare( s3, s4 )= -1
          QString::localeAwareCompare( s4, s5 )= 1
          QString::localeAwareCompare( s5, s6 )= 1
          QString::localeAwareCompare( s6, s7 )= -1
          
          
          1 Reply Last reply
          0
          • AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote last edited by AndyBrice
            #5

            Note that QCollator returns the correct result. This code:

                QCollator collator(QLocale::Swedish);
                QStringList list = {s7, s1, s2, s3, s4, s5, s6 };
                std::sort(list.begin(), list.end(), collator);
                qDebug() << "list=" << list;
            

            Returns:

            list= QList("a", "b", "o", "z", "å", "ä", "ö")
            

            In Qt 5.15.19, Qt 6.7.3 and Qt 6.10.0.

            1 Reply Last reply
            0
            • AndyBriceA Offline
              AndyBriceA Offline
              AndyBrice
              wrote last edited by
              #6

              I have added a bug report: https://bugreports.qt.io/browse/QTBUG-141463

              1 Reply Last reply
              1
              • C Offline
                C Offline
                ChrisW67
                wrote last edited by
                #7

                On Linux, online installer Qt 6.10.0 or 6.8.1, with bundled libicui18n.so.73 the result is all -1 as expected.
                With Qt 5.15.2 and libicui18n.so.56 on the same system I have this alternate version:

                s1= "a"
                s2= "o"
                s3= "o"
                s4= "z"
                s5= "å"
                s6= "ä"
                s7= "ö"
                QString::localeAwareCompare( s1, s2 )= -1
                QString::localeAwareCompare( s2, s3 )= -1
                QString::localeAwareCompare( s3, s4 )= -1
                QString::localeAwareCompare( s4, s5 )= 1
                QString::localeAwareCompare( s5, s6 )= -1
                QString::localeAwareCompare( s6, s7 )= -1
                

                So even older ICU versions display variation.

                Edit: Given the comments on the bug, I don't think this will actually help unless you use an ICU-enabled build of Qt.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved