How to sort a nested array using lodash












1















I have a collection of objects in this array and I need to order them by the 'order' key (asc). Is there a way to sort the objects inside the array and then return the whole array? I am relying on the order as I'm using it in a v-for with a :key.



[
{
"id":0,
"type":"Header",
"order":1,
"props":{
"order":0,
"id":0,
"section_id":0
},
"data":{
"header":""
},
"component":"header-block"
},
{
"id":1,
"type":"Header",
"order":0,
"props":{
"order":1,
"id":1,
"section_id":0
},
"data":{
"header":""
},
"component":"header-block"
}
],
[
//Another collection of objects
]


I am currently doing this -



getters: {
sorted: state => {
return _.orderBy(state.experience_sections, function(block) {
if(block.experience_blocks[0]) {
return block.experience_blocks[0].order;
}
});
}
}


The solution above does not seem to order the objects by 'asc' order. Am I on the right track?



Thanks!



P.S. Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. My apologies if I missed an already answered question.










share|improve this question





























    1















    I have a collection of objects in this array and I need to order them by the 'order' key (asc). Is there a way to sort the objects inside the array and then return the whole array? I am relying on the order as I'm using it in a v-for with a :key.



    [
    {
    "id":0,
    "type":"Header",
    "order":1,
    "props":{
    "order":0,
    "id":0,
    "section_id":0
    },
    "data":{
    "header":""
    },
    "component":"header-block"
    },
    {
    "id":1,
    "type":"Header",
    "order":0,
    "props":{
    "order":1,
    "id":1,
    "section_id":0
    },
    "data":{
    "header":""
    },
    "component":"header-block"
    }
    ],
    [
    //Another collection of objects
    ]


    I am currently doing this -



    getters: {
    sorted: state => {
    return _.orderBy(state.experience_sections, function(block) {
    if(block.experience_blocks[0]) {
    return block.experience_blocks[0].order;
    }
    });
    }
    }


    The solution above does not seem to order the objects by 'asc' order. Am I on the right track?



    Thanks!



    P.S. Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. My apologies if I missed an already answered question.










    share|improve this question



























      1












      1








      1








      I have a collection of objects in this array and I need to order them by the 'order' key (asc). Is there a way to sort the objects inside the array and then return the whole array? I am relying on the order as I'm using it in a v-for with a :key.



      [
      {
      "id":0,
      "type":"Header",
      "order":1,
      "props":{
      "order":0,
      "id":0,
      "section_id":0
      },
      "data":{
      "header":""
      },
      "component":"header-block"
      },
      {
      "id":1,
      "type":"Header",
      "order":0,
      "props":{
      "order":1,
      "id":1,
      "section_id":0
      },
      "data":{
      "header":""
      },
      "component":"header-block"
      }
      ],
      [
      //Another collection of objects
      ]


      I am currently doing this -



      getters: {
      sorted: state => {
      return _.orderBy(state.experience_sections, function(block) {
      if(block.experience_blocks[0]) {
      return block.experience_blocks[0].order;
      }
      });
      }
      }


      The solution above does not seem to order the objects by 'asc' order. Am I on the right track?



      Thanks!



      P.S. Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. My apologies if I missed an already answered question.










      share|improve this question
















      I have a collection of objects in this array and I need to order them by the 'order' key (asc). Is there a way to sort the objects inside the array and then return the whole array? I am relying on the order as I'm using it in a v-for with a :key.



      [
      {
      "id":0,
      "type":"Header",
      "order":1,
      "props":{
      "order":0,
      "id":0,
      "section_id":0
      },
      "data":{
      "header":""
      },
      "component":"header-block"
      },
      {
      "id":1,
      "type":"Header",
      "order":0,
      "props":{
      "order":1,
      "id":1,
      "section_id":0
      },
      "data":{
      "header":""
      },
      "component":"header-block"
      }
      ],
      [
      //Another collection of objects
      ]


      I am currently doing this -



      getters: {
      sorted: state => {
      return _.orderBy(state.experience_sections, function(block) {
      if(block.experience_blocks[0]) {
      return block.experience_blocks[0].order;
      }
      });
      }
      }


      The solution above does not seem to order the objects by 'asc' order. Am I on the right track?



      Thanks!



      P.S. Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. My apologies if I missed an already answered question.







      javascript lodash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 20:19









      George Jempty

      7,0251161132




      7,0251161132










      asked Nov 24 '18 at 19:51









      Raj SahaRaj Saha

      245




      245
























          4 Answers
          4






          active

          oldest

          votes


















          1














          You should also consider orderBy method from lodash since you could easily change from asc to desc sort order if you would want to at a later date or have it via a variable being passed through the UI etc:






          const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

          console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
          console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

          <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








          share|improve this answer

































            1














            Just in case you want plain javascript solution.. using Array.forEach



            I have also extended your array to contain more data






            var arr = [[
            {
            "id":0,
            "type":"Header",
            "order":1,
            "props":{
            "order":0,
            "id":0,
            "section_id":0
            },
            "data":{
            "header":""
            },
            "component":"header-block"
            },
            {
            "id":1,
            "type":"Header",
            "order":0,
            "props":{
            "order":1,
            "id":1,
            "section_id":0
            },
            "data":{
            "header":""
            },
            "component":"header-block"
            }
            ], [
            {
            "id":0,
            "type":"Header",
            "order":2,
            "props":{
            "order":0,
            "id":0,
            "section_id":0
            },
            "data":{
            "header":""
            },
            "component":"header-block"
            },
            {
            "id":1,
            "type":"Header",
            "order":1,
            "props":{
            "order":1,
            "id":1,
            "section_id":0
            },
            "data":{
            "header":""
            },
            "component":"header-block"
            }
            ]]

            arr.forEach(d => d.sort((a,b) => a.order - b.order))

            console.log(arr)








            share|improve this answer


























            • Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

              – Nitish Narang
              Nov 24 '18 at 20:18






            • 1





              Edited @slider :) Thanks for pointing.

              – Nitish Narang
              Nov 24 '18 at 20:19



















            1














            Will sort each subarray in an array



            const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));





            share|improve this answer

































              0














              Deep sorting using lodash



              const sortedArray = _.orderBy(items, [(item) => {
              const nestedObj = _.get(item, 'props');
              item['props'] = _.orderBy(nestedObj,'order','desc');
              return item['order'];
              }], 'desc');





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461822%2fhow-to-sort-a-nested-array-using-lodash%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                You should also consider orderBy method from lodash since you could easily change from asc to desc sort order if you would want to at a later date or have it via a variable being passed through the UI etc:






                const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

                console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
                console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

                <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








                share|improve this answer






























                  1














                  You should also consider orderBy method from lodash since you could easily change from asc to desc sort order if you would want to at a later date or have it via a variable being passed through the UI etc:






                  const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

                  console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
                  console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








                  share|improve this answer




























                    1












                    1








                    1







                    You should also consider orderBy method from lodash since you could easily change from asc to desc sort order if you would want to at a later date or have it via a variable being passed through the UI etc:






                    const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

                    console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
                    console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








                    share|improve this answer















                    You should also consider orderBy method from lodash since you could easily change from asc to desc sort order if you would want to at a later date or have it via a variable being passed through the UI etc:






                    const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

                    console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
                    console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








                    const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

                    console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
                    console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                    const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

                    console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
                    console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 25 '18 at 1:27

























                    answered Nov 25 '18 at 0:02









                    AkrionAkrion

                    9,48511224




                    9,48511224

























                        1














                        Just in case you want plain javascript solution.. using Array.forEach



                        I have also extended your array to contain more data






                        var arr = [[
                        {
                        "id":0,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":0,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ], [
                        {
                        "id":0,
                        "type":"Header",
                        "order":2,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ]]

                        arr.forEach(d => d.sort((a,b) => a.order - b.order))

                        console.log(arr)








                        share|improve this answer


























                        • Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

                          – Nitish Narang
                          Nov 24 '18 at 20:18






                        • 1





                          Edited @slider :) Thanks for pointing.

                          – Nitish Narang
                          Nov 24 '18 at 20:19
















                        1














                        Just in case you want plain javascript solution.. using Array.forEach



                        I have also extended your array to contain more data






                        var arr = [[
                        {
                        "id":0,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":0,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ], [
                        {
                        "id":0,
                        "type":"Header",
                        "order":2,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ]]

                        arr.forEach(d => d.sort((a,b) => a.order - b.order))

                        console.log(arr)








                        share|improve this answer


























                        • Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

                          – Nitish Narang
                          Nov 24 '18 at 20:18






                        • 1





                          Edited @slider :) Thanks for pointing.

                          – Nitish Narang
                          Nov 24 '18 at 20:19














                        1












                        1








                        1







                        Just in case you want plain javascript solution.. using Array.forEach



                        I have also extended your array to contain more data






                        var arr = [[
                        {
                        "id":0,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":0,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ], [
                        {
                        "id":0,
                        "type":"Header",
                        "order":2,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ]]

                        arr.forEach(d => d.sort((a,b) => a.order - b.order))

                        console.log(arr)








                        share|improve this answer















                        Just in case you want plain javascript solution.. using Array.forEach



                        I have also extended your array to contain more data






                        var arr = [[
                        {
                        "id":0,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":0,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ], [
                        {
                        "id":0,
                        "type":"Header",
                        "order":2,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ]]

                        arr.forEach(d => d.sort((a,b) => a.order - b.order))

                        console.log(arr)








                        var arr = [[
                        {
                        "id":0,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":0,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ], [
                        {
                        "id":0,
                        "type":"Header",
                        "order":2,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ]]

                        arr.forEach(d => d.sort((a,b) => a.order - b.order))

                        console.log(arr)





                        var arr = [[
                        {
                        "id":0,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":0,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ], [
                        {
                        "id":0,
                        "type":"Header",
                        "order":2,
                        "props":{
                        "order":0,
                        "id":0,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        },
                        {
                        "id":1,
                        "type":"Header",
                        "order":1,
                        "props":{
                        "order":1,
                        "id":1,
                        "section_id":0
                        },
                        "data":{
                        "header":""
                        },
                        "component":"header-block"
                        }
                        ]]

                        arr.forEach(d => d.sort((a,b) => a.order - b.order))

                        console.log(arr)






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 24 '18 at 20:19

























                        answered Nov 24 '18 at 20:11









                        Nitish NarangNitish Narang

                        2,9501815




                        2,9501815













                        • Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

                          – Nitish Narang
                          Nov 24 '18 at 20:18






                        • 1





                          Edited @slider :) Thanks for pointing.

                          – Nitish Narang
                          Nov 24 '18 at 20:19



















                        • Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

                          – Nitish Narang
                          Nov 24 '18 at 20:18






                        • 1





                          Edited @slider :) Thanks for pointing.

                          – Nitish Narang
                          Nov 24 '18 at 20:19

















                        Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

                        – Nitish Narang
                        Nov 24 '18 at 20:18





                        Bang on!! Returning new array will not do any good here.. so yes forEach can be apt here...

                        – Nitish Narang
                        Nov 24 '18 at 20:18




                        1




                        1





                        Edited @slider :) Thanks for pointing.

                        – Nitish Narang
                        Nov 24 '18 at 20:19





                        Edited @slider :) Thanks for pointing.

                        – Nitish Narang
                        Nov 24 '18 at 20:19











                        1














                        Will sort each subarray in an array



                        const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));





                        share|improve this answer






























                          1














                          Will sort each subarray in an array



                          const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));





                          share|improve this answer




























                            1












                            1








                            1







                            Will sort each subarray in an array



                            const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));





                            share|improve this answer















                            Will sort each subarray in an array



                            const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 24 '18 at 21:58

























                            answered Nov 24 '18 at 20:26









                            Jacek LipiecJacek Lipiec

                            1917




                            1917























                                0














                                Deep sorting using lodash



                                const sortedArray = _.orderBy(items, [(item) => {
                                const nestedObj = _.get(item, 'props');
                                item['props'] = _.orderBy(nestedObj,'order','desc');
                                return item['order'];
                                }], 'desc');





                                share|improve this answer




























                                  0














                                  Deep sorting using lodash



                                  const sortedArray = _.orderBy(items, [(item) => {
                                  const nestedObj = _.get(item, 'props');
                                  item['props'] = _.orderBy(nestedObj,'order','desc');
                                  return item['order'];
                                  }], 'desc');





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Deep sorting using lodash



                                    const sortedArray = _.orderBy(items, [(item) => {
                                    const nestedObj = _.get(item, 'props');
                                    item['props'] = _.orderBy(nestedObj,'order','desc');
                                    return item['order'];
                                    }], 'desc');





                                    share|improve this answer













                                    Deep sorting using lodash



                                    const sortedArray = _.orderBy(items, [(item) => {
                                    const nestedObj = _.get(item, 'props');
                                    item['props'] = _.orderBy(nestedObj,'order','desc');
                                    return item['order'];
                                    }], 'desc');






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 21 at 11:10









                                    pradeep gowdapradeep gowda

                                    42157




                                    42157






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461822%2fhow-to-sort-a-nested-array-using-lodash%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Feedback on college project

                                        Futebolista

                                        Albești (Vaslui)