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 json sort values
Forum Updated to NodeBB v4.3 + New Features

How json sort values

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 3.3k Views
  • 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.
  • F firsnur96

    @jsulm You might be right, but when i right elements in my array like
    Cherry
    Apple
    Banana

    ıt sorts them
    apple
    banana
    cherry

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #5

    @firsnur96
    Show your code for exactly what you mean by "array". As @jsulm wrote JSON should output true arrays in array order.

    1 Reply Last reply
    1
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #6

      Json is by definition unordered

      However, the QT implementation uses a QMap (IIRC). QMap alphabetically orders its keys, nothing you can do about it, but writing your own json writer


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JonBJ 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        Json is by definition unordered

        However, the QT implementation uses a QMap (IIRC). QMap alphabetically orders its keys, nothing you can do about it, but writing your own json writer

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #7

        @J-Hilk
        I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not. Only the integer indexes into the array. Which it will output to the JSON in array element order. So what are you talking about here for "array", and as I asked the OP to show code, there should be no QMap nor "cherry", "apple", "banana"?

        Or it's not an "array"....

        KroMignonK J.HilkJ 2 Replies Last reply
        0
        • JonBJ JonB

          @J-Hilk
          I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not. Only the integer indexes into the array. Which it will output to the JSON in array element order. So what are you talking about here for "array", and as I asked the OP to show code, there should be no QMap nor "cherry", "apple", "banana"?

          Or it's not an "array"....

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #8

          @JonB said in How json sort values:

          I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not.

          Of course there are keys in JSON, for example:

          {
             "index": 1,
             "value": "test",
             "parameters": ["x", "b", "c"']
          }
          

          There are 3 keys: index, value and parameters.
          Those will in a QMap, which means they are sorted.
          But the value hold by key parameters will not be sorted, this is a list.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          JonBJ 1 Reply Last reply
          2
          • JonBJ JonB

            @J-Hilk
            I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not. Only the integer indexes into the array. Which it will output to the JSON in array element order. So what are you talking about here for "array", and as I asked the OP to show code, there should be no QMap nor "cherry", "apple", "banana"?

            Or it's not an "array"....

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #9

            @JonB I was refering to the opening post:

            I have a json file and i read some values on there.
            My question is, how qt sort the json elements?

            this is ambiguous enough to mean anything I may guess it to the qt way of alphabetically sorting keys.

            the 2nd post

            Cherry
            Apple
            Banana

            ıt sorts them
            apple
            banana
            cherry

            doesn't add anything of value, those are neither lists nor arrays in any language/storage format that I know of


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • KroMignonK KroMignon

              @JonB said in How json sort values:

              I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not.

              Of course there are keys in JSON, for example:

              {
                 "index": 1,
                 "value": "test",
                 "parameters": ["x", "b", "c"']
              }
              

              There are 3 keys: index, value and parameters.
              Those will in a QMap, which means they are sorted.
              But the value hold by key parameters will not be sorted, this is a list.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #10

              @KroMignon said in How json sort values:

              @JonB said in How json sort values:

              I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not.

              Of course there are keys in JSON, for example:

              Did you read my answer? I italicised array. What you show with { ... } is not an array, it's an object. You do have one one array value there, ["x", "b", "c"']. And that should be serialized just as shown, there are no keys here.

              There are 3 keys: index, value and parameters.

              Those will in a QMap, which means they are sorted.

              Indeed. And they do not form an array or list.

              But the value hold by key parameters will not be sorted, this is a list.

              Indeed. Which is what I & @jsulm said:

              There is no sort order in JSON. Array elements come in the order they were added to the array.

              KroMignonK 1 Reply Last reply
              0
              • JonBJ JonB

                @KroMignon said in How json sort values:

                @JonB said in How json sort values:

                I'm lost. If the OP is genuinely JSON-serializing an array, in C++ terms (or list in Python), there are no "keys", alphabetical or not.

                Of course there are keys in JSON, for example:

                Did you read my answer? I italicised array. What you show with { ... } is not an array, it's an object. You do have one one array value there, ["x", "b", "c"']. And that should be serialized just as shown, there are no keys here.

                There are 3 keys: index, value and parameters.

                Those will in a QMap, which means they are sorted.

                Indeed. And they do not form an array or list.

                But the value hold by key parameters will not be sorted, this is a list.

                Indeed. Which is what I & @jsulm said:

                There is no sort order in JSON. Array elements come in the order they were added to the array.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #11

                @JonB said in How json sort values:

                Did you read my answer? I italicised array. What you show with { ... } is not an array, it's an object. You do have one one array value there, ["x", "b", "c"']. And that should be serialized just as shown, there are no keys here.

                I apologies, I am not a native speaker.
                I did read your answer, but it seems I did not right understand it.
                I know that {} is for objects and [] for arrays.
                I also know that many people mixup keys and values when speaking about JSON, and I am pretty sure this is the root failure here for this thread.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                JonBJ 1 Reply Last reply
                2
                • KroMignonK KroMignon

                  @JonB said in How json sort values:

                  Did you read my answer? I italicised array. What you show with { ... } is not an array, it's an object. You do have one one array value there, ["x", "b", "c"']. And that should be serialized just as shown, there are no keys here.

                  I apologies, I am not a native speaker.
                  I did read your answer, but it seems I did not right understand it.
                  I know that {} is for objects and [] for arrays.
                  I also know that many people mixup keys and values when speaking about JSON, and I am pretty sure this is the root failure here for this thread.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #12

                  @KroMignon
                  I think you are right. I asked the OP for clarification of the code, because "sorting" of "keys" with values "apple", "banana" & "cherry" refers to a "map" or "object" but not to an "array".

                  F 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @KroMignon
                    I think you are right. I asked the OP for clarification of the code, because "sorting" of "keys" with values "apple", "banana" & "cherry" refers to a "map" or "object" but not to an "array".

                    F Offline
                    F Offline
                    firsnur96
                    wrote on last edited by
                    #13

                    @JonB

                         "Fruits":[
                            {
                               "0":[
                                  "0",
                                  "Cherry",
                                  "---"
                               ],
                               "1":[
                                  "1",
                                  "Apple",
                                  "---"
                               ],
                               "2":[
                                  "2",
                                  "Banana",
                                  "---"
                               ]
                    

                    Here is a part of my json file, there should me missing paranthesis etc. sorry for it. Im trying to understand all answers

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • F firsnur96

                      @JonB

                           "Fruits":[
                              {
                                 "0":[
                                    "0",
                                    "Cherry",
                                    "---"
                                 ],
                                 "1":[
                                    "1",
                                    "Apple",
                                    "---"
                                 ],
                                 "2":[
                                    "2",
                                    "Banana",
                                    "---"
                                 ]
                      

                      Here is a part of my json file, there should me missing paranthesis etc. sorry for it. Im trying to understand all answers

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      @firsnur96 See the answer from @J-Hilk

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • F firsnur96

                        @JonB

                             "Fruits":[
                                {
                                   "0":[
                                      "0",
                                      "Cherry",
                                      "---"
                                   ],
                                   "1":[
                                      "1",
                                      "Apple",
                                      "---"
                                   ],
                                   "2":[
                                      "2",
                                      "Banana",
                                      "---"
                                   ]
                        

                        Here is a part of my json file, there should me missing paranthesis etc. sorry for it. Im trying to understand all answers

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #15

                        @firsnur96
                        I don't know whether what you have shown is what you have, what you want, what you don't get, or whatever.

                        Each item like

                        [
                                      "0",
                                      "Cherry",
                                      "---"
                                   ],
                        

                        is itself an array/list. But the outer element like

                                {
                                   "0": ...
                                   "1": ...
                        
                        

                        is an object with keys like "0", "1".

                        I don't know where your

                        ıt sorts them

                        apple

                        banana

                        cherry

                        comes from.

                        Maybe some structure in C++ is involved? A QMap?? I don't understand what exactly is "missorting" where.

                        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