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.0k 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 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?

    Pablo J. RoginaP 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #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
      • semlanikS Offline
        semlanikS Offline
        semlanik
        wrote on last edited by
        #2

        Inherit of QString class and redefine operator<()

        A 1 Reply Last reply
        1
        • semlanikS semlanik

          Inherit of QString class and redefine operator<()

          A Offline
          A Offline
          Alain38 0
          wrote on 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.

          JonBJ 1 Reply Last reply
          0
          • A Alain38 0

            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #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
            0
            • JonBJ JonB

              @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 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.

              JonBJ 1 Reply Last reply
              0
              • A Alain38 0

                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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • A Alain38 0

                  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?

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on 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
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by Chris Kawa
                    #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

                    • Login

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