Sort nested dictionary by value in JS









up vote
-1
down vote

favorite












Can someone please help to figure out the way to sort a dictionary with another dictionary inside in JavaScript?
This is how my dict looks like:



'POS2': 'stegano': 0, 'sum': 200, 'misc': 100, 'web': 0, 'ppc': 0, 'crypto': 0, 'admin': 0, 'vuln': 0, 'forensics': 0, 'hardware': 0, 'reverse': 0, 'recon': 100, ...



I want to sort it by 'sum' key that is stored in nested dict.
I can find some solutions written in python, but the challenge here is to achieve the same functionality in JS.
Would appreciate any help.










share|improve this question

















  • 1




    You would then expect an array as output, yes?
    – Icepickle
    Nov 11 at 12:17






  • 1




    Objects are unordered sets of properties. So when you speak of sort what output do you expect?
    – trincot
    Nov 11 at 12:17










  • Thank you, both , for your interest. At first I thought of having dictionary as an output, but having the elements inside reorganized, so the first element would have the smallest sum number. But now that @trincot has mentioned and I did my checking, I can see why that wouldn’t work. My dictionary is an object in JS, which properties are unordered. So, I suppose having an array as an output is the way to go
    – Ilja Leiko
    Nov 11 at 12:28














up vote
-1
down vote

favorite












Can someone please help to figure out the way to sort a dictionary with another dictionary inside in JavaScript?
This is how my dict looks like:



'POS2': 'stegano': 0, 'sum': 200, 'misc': 100, 'web': 0, 'ppc': 0, 'crypto': 0, 'admin': 0, 'vuln': 0, 'forensics': 0, 'hardware': 0, 'reverse': 0, 'recon': 100, ...



I want to sort it by 'sum' key that is stored in nested dict.
I can find some solutions written in python, but the challenge here is to achieve the same functionality in JS.
Would appreciate any help.










share|improve this question

















  • 1




    You would then expect an array as output, yes?
    – Icepickle
    Nov 11 at 12:17






  • 1




    Objects are unordered sets of properties. So when you speak of sort what output do you expect?
    – trincot
    Nov 11 at 12:17










  • Thank you, both , for your interest. At first I thought of having dictionary as an output, but having the elements inside reorganized, so the first element would have the smallest sum number. But now that @trincot has mentioned and I did my checking, I can see why that wouldn’t work. My dictionary is an object in JS, which properties are unordered. So, I suppose having an array as an output is the way to go
    – Ilja Leiko
    Nov 11 at 12:28












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Can someone please help to figure out the way to sort a dictionary with another dictionary inside in JavaScript?
This is how my dict looks like:



'POS2': 'stegano': 0, 'sum': 200, 'misc': 100, 'web': 0, 'ppc': 0, 'crypto': 0, 'admin': 0, 'vuln': 0, 'forensics': 0, 'hardware': 0, 'reverse': 0, 'recon': 100, ...



I want to sort it by 'sum' key that is stored in nested dict.
I can find some solutions written in python, but the challenge here is to achieve the same functionality in JS.
Would appreciate any help.










share|improve this question













Can someone please help to figure out the way to sort a dictionary with another dictionary inside in JavaScript?
This is how my dict looks like:



'POS2': 'stegano': 0, 'sum': 200, 'misc': 100, 'web': 0, 'ppc': 0, 'crypto': 0, 'admin': 0, 'vuln': 0, 'forensics': 0, 'hardware': 0, 'reverse': 0, 'recon': 100, ...



I want to sort it by 'sum' key that is stored in nested dict.
I can find some solutions written in python, but the challenge here is to achieve the same functionality in JS.
Would appreciate any help.







javascript dictionary






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 12:13









Ilja Leiko

6812




6812







  • 1




    You would then expect an array as output, yes?
    – Icepickle
    Nov 11 at 12:17






  • 1




    Objects are unordered sets of properties. So when you speak of sort what output do you expect?
    – trincot
    Nov 11 at 12:17










  • Thank you, both , for your interest. At first I thought of having dictionary as an output, but having the elements inside reorganized, so the first element would have the smallest sum number. But now that @trincot has mentioned and I did my checking, I can see why that wouldn’t work. My dictionary is an object in JS, which properties are unordered. So, I suppose having an array as an output is the way to go
    – Ilja Leiko
    Nov 11 at 12:28












  • 1




    You would then expect an array as output, yes?
    – Icepickle
    Nov 11 at 12:17






  • 1




    Objects are unordered sets of properties. So when you speak of sort what output do you expect?
    – trincot
    Nov 11 at 12:17










  • Thank you, both , for your interest. At first I thought of having dictionary as an output, but having the elements inside reorganized, so the first element would have the smallest sum number. But now that @trincot has mentioned and I did my checking, I can see why that wouldn’t work. My dictionary is an object in JS, which properties are unordered. So, I suppose having an array as an output is the way to go
    – Ilja Leiko
    Nov 11 at 12:28







1




1




You would then expect an array as output, yes?
– Icepickle
Nov 11 at 12:17




You would then expect an array as output, yes?
– Icepickle
Nov 11 at 12:17




1




1




Objects are unordered sets of properties. So when you speak of sort what output do you expect?
– trincot
Nov 11 at 12:17




Objects are unordered sets of properties. So when you speak of sort what output do you expect?
– trincot
Nov 11 at 12:17












Thank you, both , for your interest. At first I thought of having dictionary as an output, but having the elements inside reorganized, so the first element would have the smallest sum number. But now that @trincot has mentioned and I did my checking, I can see why that wouldn’t work. My dictionary is an object in JS, which properties are unordered. So, I suppose having an array as an output is the way to go
– Ilja Leiko
Nov 11 at 12:28




Thank you, both , for your interest. At first I thought of having dictionary as an output, but having the elements inside reorganized, so the first element would have the smallest sum number. But now that @trincot has mentioned and I did my checking, I can see why that wouldn’t work. My dictionary is an object in JS, which properties are unordered. So, I suppose having an array as an output is the way to go
– Ilja Leiko
Nov 11 at 12:28












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Just retrieve the inner object using Object.values and then use Array.sort to sort by it's content. Note that you cannot sort an objects properties as they are per default an unsorted set (or at least, it's not properly described how properties should be sorted on an object)






const dict = 
'POS2':
'stegano': 0,
'sum': 200,
'misc': 100,
'web': 0,
'ppc': 0,
'crypto': 0,
'admin': 0,
'vuln': 0,
'forensics': 0,
'hardware': 0,
'reverse': 0,
'recon': 100
,
'POS1':
'sum': 100
,
'POS3':
'sum': 250

;

function orderBySubKey( input, key )
return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


console.log( orderBySubKey( dict, 'sum' ) );





This sample would remove the outer properties, if you want to keep these, you can change your ordering a bit, for example like






const dict = 
'POS2':
'stegano': 0,
'sum': 200,
'misc': 100,
'web': 0,
'ppc': 0,
'crypto': 0,
'admin': 0,
'vuln': 0,
'forensics': 0,
'hardware': 0,
'reverse': 0,
'recon': 100
,
'POS1':
'sum': 100
,
'POS3':
'sum': 250

;

function orderBySubKey( input, key )
return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


console.log( orderBySubKey( dict, 'sum' ) );





Which gives you a different output, but would still allow you to lookup inside the original dictionary






share|improve this answer



























    up vote
    0
    down vote













    Since you can't order objects by property you'd need to map the objects to an array and then sort that. And you'd probably not want to lose the key for each nested object (like POS2), so you'd want to embed that key in the object for safe-keeping.






    const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

    // map over the object keys
    const out = Object.keys(obj).map(key =>

    // on each iteration return the object with its new key property
    return ...obj[key], key

    // then sort the array of object by their sums
    ).sort((a, b) => a.sum - b.sum);

    console.log(out);








    share|improve this answer



























      up vote
      0
      down vote













      For example, you have next array (similar to your array, but has less properties to readable):



      let obj = 
      pos1: sum: 100, name: 'p1' ,
      pos2: sum: 1, name: 'p2' ,
      pos3: sum: 50, name: 'p3'
      ;

      var result = Object.keys(obj)
      .map(key => return key, val: obj[key]) // output: unsorted array
      .sort((a, b) => a.val.sum > b.val.sum);

      console.log(result);


      and as result you will see:



      [ key: 'pos2', val: sum: 1, name: 'p2' ,
      key: 'pos3', val: sum: 50, name: 'p3' ,
      key: 'pos1', val: sum: 100, name: 'p1' ]





      share|improve this answer






















      • And what if the OP wanted to keep the key names like POS2?
        – Andy
        Nov 11 at 12:22










      • @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
        – Sergey Shulik
        Nov 11 at 12:41










      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',
      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%2f53248638%2fsort-nested-dictionary-by-value-in-js%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Just retrieve the inner object using Object.values and then use Array.sort to sort by it's content. Note that you cannot sort an objects properties as they are per default an unsorted set (or at least, it's not properly described how properties should be sorted on an object)






      const dict = 
      'POS2':
      'stegano': 0,
      'sum': 200,
      'misc': 100,
      'web': 0,
      'ppc': 0,
      'crypto': 0,
      'admin': 0,
      'vuln': 0,
      'forensics': 0,
      'hardware': 0,
      'reverse': 0,
      'recon': 100
      ,
      'POS1':
      'sum': 100
      ,
      'POS3':
      'sum': 250

      ;

      function orderBySubKey( input, key )
      return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


      console.log( orderBySubKey( dict, 'sum' ) );





      This sample would remove the outer properties, if you want to keep these, you can change your ordering a bit, for example like






      const dict = 
      'POS2':
      'stegano': 0,
      'sum': 200,
      'misc': 100,
      'web': 0,
      'ppc': 0,
      'crypto': 0,
      'admin': 0,
      'vuln': 0,
      'forensics': 0,
      'hardware': 0,
      'reverse': 0,
      'recon': 100
      ,
      'POS1':
      'sum': 100
      ,
      'POS3':
      'sum': 250

      ;

      function orderBySubKey( input, key )
      return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


      console.log( orderBySubKey( dict, 'sum' ) );





      Which gives you a different output, but would still allow you to lookup inside the original dictionary






      share|improve this answer
























        up vote
        0
        down vote













        Just retrieve the inner object using Object.values and then use Array.sort to sort by it's content. Note that you cannot sort an objects properties as they are per default an unsorted set (or at least, it's not properly described how properties should be sorted on an object)






        const dict = 
        'POS2':
        'stegano': 0,
        'sum': 200,
        'misc': 100,
        'web': 0,
        'ppc': 0,
        'crypto': 0,
        'admin': 0,
        'vuln': 0,
        'forensics': 0,
        'hardware': 0,
        'reverse': 0,
        'recon': 100
        ,
        'POS1':
        'sum': 100
        ,
        'POS3':
        'sum': 250

        ;

        function orderBySubKey( input, key )
        return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


        console.log( orderBySubKey( dict, 'sum' ) );





        This sample would remove the outer properties, if you want to keep these, you can change your ordering a bit, for example like






        const dict = 
        'POS2':
        'stegano': 0,
        'sum': 200,
        'misc': 100,
        'web': 0,
        'ppc': 0,
        'crypto': 0,
        'admin': 0,
        'vuln': 0,
        'forensics': 0,
        'hardware': 0,
        'reverse': 0,
        'recon': 100
        ,
        'POS1':
        'sum': 100
        ,
        'POS3':
        'sum': 250

        ;

        function orderBySubKey( input, key )
        return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


        console.log( orderBySubKey( dict, 'sum' ) );





        Which gives you a different output, but would still allow you to lookup inside the original dictionary






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Just retrieve the inner object using Object.values and then use Array.sort to sort by it's content. Note that you cannot sort an objects properties as they are per default an unsorted set (or at least, it's not properly described how properties should be sorted on an object)






          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          This sample would remove the outer properties, if you want to keep these, you can change your ordering a bit, for example like






          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          Which gives you a different output, but would still allow you to lookup inside the original dictionary






          share|improve this answer












          Just retrieve the inner object using Object.values and then use Array.sort to sort by it's content. Note that you cannot sort an objects properties as they are per default an unsorted set (or at least, it's not properly described how properties should be sorted on an object)






          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          This sample would remove the outer properties, if you want to keep these, you can change your ordering a bit, for example like






          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          Which gives you a different output, but would still allow you to lookup inside the original dictionary






          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


          console.log( orderBySubKey( dict, 'sum' ) );





          const dict = 
          'POS2':
          'stegano': 0,
          'sum': 200,
          'misc': 100,
          'web': 0,
          'ppc': 0,
          'crypto': 0,
          'admin': 0,
          'vuln': 0,
          'forensics': 0,
          'hardware': 0,
          'reverse': 0,
          'recon': 100
          ,
          'POS1':
          'sum': 100
          ,
          'POS3':
          'sum': 250

          ;

          function orderBySubKey( input, key )
          return Object.keys( input ).map( key => ( key, value: input[key] ) ).sort( (a, b) => a.value[key] - b.value[key] );


          console.log( orderBySubKey( dict, 'sum' ) );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 12:24









          Icepickle

          8,43232034




          8,43232034






















              up vote
              0
              down vote













              Since you can't order objects by property you'd need to map the objects to an array and then sort that. And you'd probably not want to lose the key for each nested object (like POS2), so you'd want to embed that key in the object for safe-keeping.






              const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

              // map over the object keys
              const out = Object.keys(obj).map(key =>

              // on each iteration return the object with its new key property
              return ...obj[key], key

              // then sort the array of object by their sums
              ).sort((a, b) => a.sum - b.sum);

              console.log(out);








              share|improve this answer
























                up vote
                0
                down vote













                Since you can't order objects by property you'd need to map the objects to an array and then sort that. And you'd probably not want to lose the key for each nested object (like POS2), so you'd want to embed that key in the object for safe-keeping.






                const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

                // map over the object keys
                const out = Object.keys(obj).map(key =>

                // on each iteration return the object with its new key property
                return ...obj[key], key

                // then sort the array of object by their sums
                ).sort((a, b) => a.sum - b.sum);

                console.log(out);








                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Since you can't order objects by property you'd need to map the objects to an array and then sort that. And you'd probably not want to lose the key for each nested object (like POS2), so you'd want to embed that key in the object for safe-keeping.






                  const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

                  // map over the object keys
                  const out = Object.keys(obj).map(key =>

                  // on each iteration return the object with its new key property
                  return ...obj[key], key

                  // then sort the array of object by their sums
                  ).sort((a, b) => a.sum - b.sum);

                  console.log(out);








                  share|improve this answer












                  Since you can't order objects by property you'd need to map the objects to an array and then sort that. And you'd probably not want to lose the key for each nested object (like POS2), so you'd want to embed that key in the object for safe-keeping.






                  const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

                  // map over the object keys
                  const out = Object.keys(obj).map(key =>

                  // on each iteration return the object with its new key property
                  return ...obj[key], key

                  // then sort the array of object by their sums
                  ).sort((a, b) => a.sum - b.sum);

                  console.log(out);








                  const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

                  // map over the object keys
                  const out = Object.keys(obj).map(key =>

                  // on each iteration return the object with its new key property
                  return ...obj[key], key

                  // then sort the array of object by their sums
                  ).sort((a, b) => a.sum - b.sum);

                  console.log(out);





                  const obj = "POS2":"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS3":"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS4":"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100,"POS5":"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100

                  // map over the object keys
                  const out = Object.keys(obj).map(key =>

                  // on each iteration return the object with its new key property
                  return ...obj[key], key

                  // then sort the array of object by their sums
                  ).sort((a, b) => a.sum - b.sum);

                  console.log(out);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 12:36









                  Andy

                  27.8k63160




                  27.8k63160




















                      up vote
                      0
                      down vote













                      For example, you have next array (similar to your array, but has less properties to readable):



                      let obj = 
                      pos1: sum: 100, name: 'p1' ,
                      pos2: sum: 1, name: 'p2' ,
                      pos3: sum: 50, name: 'p3'
                      ;

                      var result = Object.keys(obj)
                      .map(key => return key, val: obj[key]) // output: unsorted array
                      .sort((a, b) => a.val.sum > b.val.sum);

                      console.log(result);


                      and as result you will see:



                      [ key: 'pos2', val: sum: 1, name: 'p2' ,
                      key: 'pos3', val: sum: 50, name: 'p3' ,
                      key: 'pos1', val: sum: 100, name: 'p1' ]





                      share|improve this answer






















                      • And what if the OP wanted to keep the key names like POS2?
                        – Andy
                        Nov 11 at 12:22










                      • @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
                        – Sergey Shulik
                        Nov 11 at 12:41














                      up vote
                      0
                      down vote













                      For example, you have next array (similar to your array, but has less properties to readable):



                      let obj = 
                      pos1: sum: 100, name: 'p1' ,
                      pos2: sum: 1, name: 'p2' ,
                      pos3: sum: 50, name: 'p3'
                      ;

                      var result = Object.keys(obj)
                      .map(key => return key, val: obj[key]) // output: unsorted array
                      .sort((a, b) => a.val.sum > b.val.sum);

                      console.log(result);


                      and as result you will see:



                      [ key: 'pos2', val: sum: 1, name: 'p2' ,
                      key: 'pos3', val: sum: 50, name: 'p3' ,
                      key: 'pos1', val: sum: 100, name: 'p1' ]





                      share|improve this answer






















                      • And what if the OP wanted to keep the key names like POS2?
                        – Andy
                        Nov 11 at 12:22










                      • @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
                        – Sergey Shulik
                        Nov 11 at 12:41












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      For example, you have next array (similar to your array, but has less properties to readable):



                      let obj = 
                      pos1: sum: 100, name: 'p1' ,
                      pos2: sum: 1, name: 'p2' ,
                      pos3: sum: 50, name: 'p3'
                      ;

                      var result = Object.keys(obj)
                      .map(key => return key, val: obj[key]) // output: unsorted array
                      .sort((a, b) => a.val.sum > b.val.sum);

                      console.log(result);


                      and as result you will see:



                      [ key: 'pos2', val: sum: 1, name: 'p2' ,
                      key: 'pos3', val: sum: 50, name: 'p3' ,
                      key: 'pos1', val: sum: 100, name: 'p1' ]





                      share|improve this answer














                      For example, you have next array (similar to your array, but has less properties to readable):



                      let obj = 
                      pos1: sum: 100, name: 'p1' ,
                      pos2: sum: 1, name: 'p2' ,
                      pos3: sum: 50, name: 'p3'
                      ;

                      var result = Object.keys(obj)
                      .map(key => return key, val: obj[key]) // output: unsorted array
                      .sort((a, b) => a.val.sum > b.val.sum);

                      console.log(result);


                      and as result you will see:



                      [ key: 'pos2', val: sum: 1, name: 'p2' ,
                      key: 'pos3', val: sum: 50, name: 'p3' ,
                      key: 'pos1', val: sum: 100, name: 'p1' ]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 11 at 12:41

























                      answered Nov 11 at 12:20









                      Sergey Shulik

                      678824




                      678824











                      • And what if the OP wanted to keep the key names like POS2?
                        – Andy
                        Nov 11 at 12:22










                      • @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
                        – Sergey Shulik
                        Nov 11 at 12:41
















                      • And what if the OP wanted to keep the key names like POS2?
                        – Andy
                        Nov 11 at 12:22










                      • @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
                        – Sergey Shulik
                        Nov 11 at 12:41















                      And what if the OP wanted to keep the key names like POS2?
                      – Andy
                      Nov 11 at 12:22




                      And what if the OP wanted to keep the key names like POS2?
                      – Andy
                      Nov 11 at 12:22












                      @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
                      – Sergey Shulik
                      Nov 11 at 12:41




                      @Andy yes, you right. i missed that input parameter is object, not array. I fixed my answer
                      – Sergey Shulik
                      Nov 11 at 12:41

















                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53248638%2fsort-nested-dictionary-by-value-in-js%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党