title: Algorithm for sorting array of objects by a nested value
body: <p>Im trying to write an algorithm for sorting objects in an array by a value in the objects. This is obviously easy to do, however I am trying to make the function flexible so it will sort by any value I pass in as the first parameter.</p>
  
  <p><strong><em>So lets say I have the following data structure:</em></strong></p>
  
  <pre><code>var data = [
    {
      employee: {
        age: 12,
        paylevel: {
          plevel: '2'
        }
      }
    },
    {
      employee: {
        age: 19,
        paylevel: {
          level: '1'
        }
      }
    }
  ]
  </code></pre>
  
  <p>I want to be able call the function as seen below and sort for any given value and sort by that specific passed in value.</p>
  
  <pre><code>sort('level', data);
  </code></pre>
  
  <p>Or </p>
  
  <pre><code>sort('age', data);
  </code></pre>
  
  <p><strong><em>Here is my working version:</em></strong></p>
  
  <pre><code>function sort(prop, data) {
    var key = prop;
    var parts = [];
  
    function _r(data) {
      if (Array.isArray(data)) {
        for (var i = 0; i &lt; data.length; i++) {
          _r(data[i]);
        }
      } else if (typeof data === 'object') {
        for (var property in data) {
          if (data.hasOwnProperty(property) &amp;&amp; parts.indexOf(property) === -1) {
            if (property === key) {
              parts.push(property);
              break;
            } else {
              if (typeof data[property] === 'object') parts.push(property);
              _r(data[property]);
            }
          }
        }
      }
    }
  
    _r(data);
  
    data.sort((a, d) =&gt; {
      var i = 0;
      while (i &lt; parts.length) {
        a = a[parts[i]];
        d = d[parts[i]];
        i++;
      }
      return a - d;
    });
  
    return data;
  }
  
  var res = sort('glevel', data); 
  </code></pre>
  
  <p>I am basically using a recursive function to attempt to build the tree structure leading down to the passed in key while adding the parent keys to an array to be used later for sorting.</p>
  
  <p>So if I pass in level, the recursive function (_r) will build a parts array with the following:</p>
  
  <pre><code>['employee', 'paylevel', 'level'] 
  </code></pre>
  
  <p>Then I use that to sort the array by looping into the data structure with a while loop and applying the javascript sort method to the results. This works great with the given data structure. But if I take the current data structure and add another object with a child key/value pair it breaks. So the following data structure will break my code:</p>
  
  <pre><code>[
    {
      employee: {
        age: 12,
        paylevel: {
          level: '1'
        },
        grouplevel: {
          glevel: '1'
        }
      }
    },
    {
      employee: {
        age: 19,
        paylevel: {
          level: '3'
        },
        grouplevel: {
          glevel: '2'
        }
      }
    }
  ]
  </code></pre>
  
  <p><strong><em>Because the parts array now looks like this:</em></strong></p>
  
  <pre><code>['employee', 'paylevel', 'level', 'glevel']
  </code></pre>
  
  <p>Adding the 'glevel' key to the array basically breaks the ability of the while loop to identify the correct item to sort. Is there a way to take a given key and build a tree structure of the parent objects and only include the parent keys:</p>
  
  <p>I hope this wasn't confusing. Thanks. </p>