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.2k 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
    4 May 2021, 07:50

    Hi everyone.
    I have a json file and i read some values on there.
    My question is, how qt sort the json elements?
    In alphabetic order or something else?
    And can we change it, because the issues getting harder for me for the sorting value

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 4 May 2021, 07:56 last edited by
    #2

    @firsnur96 There is no sort order in JSON. Array elements come in the order they were added to the array. There is no sort order for elements in an JSON object. If you need a specific sort order you have to do that on your side.

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

    F 1 Reply Last reply 4 May 2021, 08:04
    2
    • J jsulm
      4 May 2021, 07:56

      @firsnur96 There is no sort order in JSON. Array elements come in the order they were added to the array. There is no sort order for elements in an JSON object. If you need a specific sort order you have to do that on your side.

      F Offline
      F Offline
      firsnur96
      wrote on 4 May 2021, 08:04 last edited by
      #3

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

      ıt sorts them
      apple
      banana
      cherry

      J J 2 Replies Last reply 4 May 2021, 08:06
      0
      • F firsnur96
        4 May 2021, 08:04

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

        ıt sorts them
        apple
        banana
        cherry

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 4 May 2021, 08:06 last edited by
        #4

        @firsnur96 Please show your JSON and how you read it.

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

        1 Reply Last reply
        1
        • F firsnur96
          4 May 2021, 08:04

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

          ıt sorts them
          apple
          banana
          cherry

          J Offline
          J Offline
          JonB
          wrote on 4 May 2021, 08:06 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 Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 4 May 2021, 08:48 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.

            J 1 Reply Last reply 4 May 2021, 09:03
            1
            • J J.Hilk
              4 May 2021, 08:48

              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

              J Offline
              J Offline
              JonB
              wrote on 4 May 2021, 09:03 last edited by JonB 5 Apr 2021, 09:04
              #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"....

              K J 2 Replies Last reply 4 May 2021, 09:09
              0
              • J JonB
                4 May 2021, 09:03

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

                K Offline
                K Offline
                KroMignon
                wrote on 4 May 2021, 09:09 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)

                J 1 Reply Last reply 4 May 2021, 09:34
                2
                • J JonB
                  4 May 2021, 09:03

                  @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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 4 May 2021, 09:14 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
                  • K KroMignon
                    4 May 2021, 09:09

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 4 May 2021, 09:34 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.

                    K 1 Reply Last reply 4 May 2021, 09:41
                    0
                    • J JonB
                      4 May 2021, 09:34

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

                      K Offline
                      K Offline
                      KroMignon
                      wrote on 4 May 2021, 09:41 last edited by KroMignon 5 Apr 2021, 09:41
                      #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)

                      J 1 Reply Last reply 4 May 2021, 09:46
                      2
                      • K KroMignon
                        4 May 2021, 09:41

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 4 May 2021, 09:46 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 4 May 2021, 10:27
                        0
                        • J JonB
                          4 May 2021, 09:46

                          @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 4 May 2021, 10:27 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

                          J J 2 Replies Last reply 4 May 2021, 10:32
                          0
                          • F firsnur96
                            4 May 2021, 10:27

                            @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

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 4 May 2021, 10:32 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
                              4 May 2021, 10:27

                              @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

                              J Offline
                              J Offline
                              JonB
                              wrote on 4 May 2021, 18:02 last edited by JonB 5 Apr 2021, 18:05
                              #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

                              11/15

                              4 May 2021, 09:41

                              • Login

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