Results 1 to 4 of 4

Thread: QMap sorting according to QLocale

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QMap sorting according to QLocale

    Is it possible to convince a QMap<QString, QString> to sort its items according to my program's locale?
    (The program's locale is not necessarily the same as the system's locale.)

    My keys are a bunch of Names of which some start with/contain german umlauts.
    They appear in wrong order:
    E.g., iterating through, an "Ömer" should appear after "Othello", not after "Zinn" as it does right now.

  2. #2
    Join Date
    Aug 2012
    Location
    Montreal
    Posts
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap sorting according to QLocale

    I believe that for performance reason, QMap probably doesn't use locales to sort and only relies on the numerical value of UTF-16 characters. What you could do is to sort the list of keys using a combination of qsort and QString::localeAwareCompare like so:

    Qt Code:
    1. QList<QString> keyList = myMap.keys();
    2. qSort(keyList.begin(), keyList.end(), QString::localeAwareCompare);
    3. foreach(QString key, keyList)
    4. {
    5. const auto& data = myMap[key];
    6. // do something...
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to maximebd for this useful post:

    sedi (31st January 2013)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMap sorting according to QLocale

    Or perhaps something like this is acceptable in your application:
    Qt Code:
    1. class LocaleString: public QString {
    2. public:
    3. LocaleString(const char *s): QString(s) { }
    4. LocaleString(const QString &s): QString(s) { }
    5. };
    6.  
    7. inline bool operator<(const LocaleString &lhs, const LocaleString &rhs)
    8. {
    9. return (QString::localeAwareCompare(lhs, rhs) < 0);
    10. }
    11.  
    12.  
    13. // then
    14. QMap<LocaleString,int> map;
    15. map.insert(QString::fromUtf8("Ömer"), 1);
    16. map.insert("Othello", 2);
    17. map.insert("Zinn", 3);
    18. foreach(const LocaleString &s, map.keys())
    19. qDebug() << s;
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    sedi (31st January 2013)

  6. #4
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QMap sorting according to QLocale

    Thank you both very much for your help!

    You've helped me getting it solved indeed!

    My method shall return a QStringList with names. Those names should be put together from first and family name in a user definable order
    (either "Gates, Bill" or rather "Steve Jobs") and a sorting depending on first or family name.

    I simply canNOT use a QMap for such a sorting job, which I did (not knowing alternatives at that time).
    I'd actually constructed a really messy and overly complicated thing in the first place.

    Being fairly new to OOP I'd missed the obvious thing to do: create a "Name" class with some getter and setter methods and an overloaded operator< and then do a simple sort on a QList<Name>.
    According to your ideas and the QMap docs I've implemented it like this way:
    Qt Code:
    1. const inline bool operator<(const Name &n1, const Name &n2)
    2. {
    3. int comp;
    4. if (n1.sortingOrder()==Name::familyFirst)
    5. {
    6. comp=QString::localeAwareCompare(n1.family(),n2.family());
    7. if (comp!=0)
    8. return comp < 0;
    9. comp=QString::localeAwareCompare(n1.first(),n2.first());
    10. if (comp!=0)
    11. return comp < 0;
    12. comp=QString::localeAwareCompare(n1.id(),n2.id());
    13. return comp<0;
    14. } else {
    15. comp=QString::localeAwareCompare(n1.first(),n2.first());
    16. if (comp!=0)
    17. return comp < 0;
    18. comp=QString::localeAwareCompare(n1.family(),n2.family());
    19. if (comp!=0)
    20. return comp < 0;
    21. comp=QString::localeAwareCompare(n1.id(),n2.id());
    22. return comp<0;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    I now build a list of this class and just sort it:
    Qt Code:
    1. QList<Name> nameList;
    2. //fill the list, e.g. like this, but in a loop:
    3. //nameList.append(Name(first, family, id);
    4. //nameList.last().setSortingOrder(Name::familyFirst);
    5. qSort(nameList);
    To copy to clipboard, switch view to plain text mode 

    This has *so* many advantages over my old approach...

    Thank you!

Similar Threads

  1. edit QLocale
    By kito in forum Qt Programming
    Replies: 3
    Last Post: 7th January 2013, 13:08
  2. QLocale and date
    By jiveaxe in forum Newbie
    Replies: 2
    Last Post: 21st May 2012, 09:37
  3. How to disable sorting option in QMap
    By baluk in forum Newbie
    Replies: 12
    Last Post: 1st October 2010, 10:10
  4. QLocale/Resource
    By coderbob in forum Newbie
    Replies: 4
    Last Post: 21st November 2007, 20:51
  5. QLocale confusion :(
    By gri in forum Qt Programming
    Replies: 6
    Last Post: 15th June 2007, 13:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.