Why isn't QString::localeAwareCompare() workin

2019-07-12 04:30发布

问题:

A Finnish customer tells me that the correct (locale aware) sorting for Finnish is: a-z, å, ä, ö.

But QString::localeAwareCompare() returns it as: a, ä, å, b-o, ö, p-z in Qt 4.4.2 on Windows Vista.

#include <qapplication.h>
#include <qlocale.h>
#include <iostream>
#include <tchar.h>

int
main( int argc, char* argv[] )
{
    QLocale::setDefault ( QLocale( QLocale::Finnish ) );

    QApplication app( argc, argv );    

    QString s1 = "a";
    QString s2 = "ä";
    QString s3 = "å";
    QString s4 = "b";
    QString s5 = "o";
    QString s6 = "ö";
    QString s7 = "z";

    std::cout << "QString::localeAwareCompare( s1, s2 )=" << QString::localeAwareCompare( s1, s2 ) << "\n";
    std::cout << "QString::localeAwareCompare( s2, s3 )=" << QString::localeAwareCompare( s2, s3 ) << "\n";
    std::cout << "QString::localeAwareCompare( s3, s4 )=" << QString::localeAwareCompare( s3, s4 ) << "\n";
    std::cout << "QString::localeAwareCompare( s4, s5 )=" << QString::localeAwareCompare( s4, s5 ) << "\n";
    std::cout << "QString::localeAwareCompare( s5, s6 )=" << QString::localeAwareCompare( s5, s6 ) << "\n";
    std::cout << "QString::localeAwareCompare( s5, s6 )=" << QString::localeAwareCompare( s6, s7 ) << "\n";

    return app.exec();
}

Outputs:

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( s5, s6 )=-1

My Finnish customer is getting the same result. Is this a bug or am I doing something wrong?

回答1:

I tested your code in Qt 4.6.2 on Windows XP and those characters are sorted correctly. With your test case I get the following output:

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( s5, s6 )=1

And as a Finnish, I can confirm that your customer is right about the Finnish sorting order.

So maybe you could upgrade to a new Qt version?