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. How to sort a QMap<QString, ...> with a custom string order?
Forum Updated to NodeBB v4.3 + New Features

How to sort a QMap<QString, ...> with a custom string order?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 2.1k Views 3 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.
  • A Offline
    A Offline
    Alain38 0
    wrote on 9 Jan 2020, 12:12 last edited by
    #1

    Hi,
    I have to create a QMap<QString, MyStruct>. The key string is a proprietary coding syntax that do not follow the alphabetical order (too easy otherwise) but has its own order rules. So, I need that my QMap uses this specific syntax. To give you an idea the code is "one letter + number". For the letters I have rules like 'C' < 'A' < 'W' < 'T'. And for the number we are using reverse ordering (12 < 5). And the code is always stored in a QString.

    So, my problem is to inject this special order in the QMap. I have seen an option, by internally using a QString container class with its own operator<(). But, does it exist an option by just creating the operator<() on QString, like it is possible with std::map?

    P 1 Reply Last reply 9 Jan 2020, 13:02
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 9 Jan 2020, 13:58 last edited by Chris Kawa 1 Sept 2020, 13:59
      #8

      You're trying to fit a square box into a round hole. QMap simply doesn't (publicly) support custom comparators. Wrapping the type is an unnecessary overhead. Adding custom operator< in global scope is harmful pollution. QMap has an undocumented mechanism for customizing comparison function by providing your own qMapLessThanKey implementation, but it's still a bad idea.

      Just use a container suited for the task e.g. std::map that allows for custom comparators.

      1 Reply Last reply
      5
      • S Offline
        S Offline
        semlanik
        wrote on 9 Jan 2020, 12:21 last edited by
        #2

        Inherit of QString class and redefine operator<()

        A 1 Reply Last reply 9 Jan 2020, 12:27
        1
        • S semlanik
          9 Jan 2020, 12:21

          Inherit of QString class and redefine operator<()

          A Offline
          A Offline
          Alain38 0
          wrote on 9 Jan 2020, 12:27 last edited by
          #3

          Hi @semlanik,
          As it is a shared development, the inputs I have are QString. So, It is just internally that I can use another type. So, using a struct container or create a subclass of QString is equivalent in this case.

          J 1 Reply Last reply 9 Jan 2020, 12:37
          0
          • A Alain38 0
            9 Jan 2020, 12:27

            Hi @semlanik,
            As it is a shared development, the inputs I have are QString. So, It is just internally that I can use another type. So, using a struct container or create a subclass of QString is equivalent in this case.

            J Offline
            J Offline
            JonB
            wrote on 9 Jan 2020, 12:37 last edited by JonB 1 Sept 2020, 14:19
            #4

            @Alain38-0
            I do not understand whether you are agreeing with @semlanik's solution or not. Since QMap requires QString keys and sorts on them, without providing its own comparison operator, it seems to me you will have to follow his suggestion of sub-classing QString when you put the keys into the map?

            EDIT Apologies, I must have mis-read QMap key type. Ignore me (left here because you responded, deleted my later follow-up). Follow @Pablo-J-Rogina and/or @Chris-Kawa below.

            A 1 Reply Last reply 9 Jan 2020, 12:43
            0
            • J JonB
              9 Jan 2020, 12:37

              @Alain38-0
              I do not understand whether you are agreeing with @semlanik's solution or not. Since QMap requires QString keys and sorts on them, without providing its own comparison operator, it seems to me you will have to follow his suggestion of sub-classing QString when you put the keys into the map?

              EDIT Apologies, I must have mis-read QMap key type. Ignore me (left here because you responded, deleted my later follow-up). Follow @Pablo-J-Rogina and/or @Chris-Kawa below.

              A Offline
              A Offline
              Alain38 0
              wrote on 9 Jan 2020, 12:43 last edited by
              #5

              Hi @JonB,
              What I said is, have an internal structure StringContainer embedding a QString and create an operator between StringContainer, or have an internal class SubString : public QString embedding the operator is equivalent. For me, the only difference is that, with the StringContainer I'm sure that the code will never use the QString comparaison operator.

              J 1 Reply Last reply 9 Jan 2020, 12:52
              0
              • A Alain38 0
                9 Jan 2020, 12:43

                Hi @JonB,
                What I said is, have an internal structure StringContainer embedding a QString and create an operator between StringContainer, or have an internal class SubString : public QString embedding the operator is equivalent. For me, the only difference is that, with the StringContainer I'm sure that the code will never use the QString comparaison operator.

                J Offline
                J Offline
                JonB
                wrote on 9 Jan 2020, 12:52 last edited by JonB 1 Sept 2020, 12:55
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • A Alain38 0
                  9 Jan 2020, 12:12

                  Hi,
                  I have to create a QMap<QString, MyStruct>. The key string is a proprietary coding syntax that do not follow the alphabetical order (too easy otherwise) but has its own order rules. So, I need that my QMap uses this specific syntax. To give you an idea the code is "one letter + number". For the letters I have rules like 'C' < 'A' < 'W' < 'T'. And for the number we are using reverse ordering (12 < 5). And the code is always stored in a QString.

                  So, my problem is to inject this special order in the QMap. I have seen an option, by internally using a QString container class with its own operator<(). But, does it exist an option by just creating the operator<() on QString, like it is possible with std::map?

                  P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 9 Jan 2020, 13:02 last edited by
                  #7

                  @Alain38-0 said in How to sort a QMap<QString, ...> with a custom string order?:

                  The key string is a proprietary coding syntax that do not follow the alphabetical order (too easy otherwise) but has its own order rules. So, I need that my QMap uses this specific syntax.

                  If it's an "special" string, then create an "special" class to cope with such own order rules. If not, it's a plain QString and it will follow expected order rules.

                  As @semlanik said, you need to redefine operator<(). From QMap documentation:

                  In addition, QMap's key type must provide operator<(). QMap uses it to keep its items sorted, and assumes that two keys x and y are equal if neither x < y nor y < x is true.

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  1
                  • C Online
                    C Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 9 Jan 2020, 13:58 last edited by Chris Kawa 1 Sept 2020, 13:59
                    #8

                    You're trying to fit a square box into a round hole. QMap simply doesn't (publicly) support custom comparators. Wrapping the type is an unnecessary overhead. Adding custom operator< in global scope is harmful pollution. QMap has an undocumented mechanism for customizing comparison function by providing your own qMapLessThanKey implementation, but it's still a bad idea.

                    Just use a container suited for the task e.g. std::map that allows for custom comparators.

                    1 Reply Last reply
                    5

                    1/8

                    9 Jan 2020, 12:12

                    • Login

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