title: custom keys sorting in rolled data d3.js
body: <p>I try to sort keys of nested data taken from a csv file using <code>d3.js</code>. The thing is, that it's not possible at this case to sort by <code>d3.ascending</code> or <code>d3.descending</code> because my values are strings.</p>
  
  <p>I found example, which fits my needs:</p>
  
  <pre><code>var priority_order = ['MUST', "SHOULD", 'COULD', 'WISH'];
  
  var nested_data = d3.nest()
      .key(function(d) { return d.status; })
      .sortKeys(d3.ascending)
      .key(function(d) { return d.priority; })
      .sortKeys(function(a,b) { return priority_order.indexOf(a) - priority_order.indexOf(b); })
      .rollup(function(leaves) { return leaves.length; })
      .entries(csv_data);
  </code></pre>
  
  <p>But it doesn't work when I use it. I don't get well the idea of <code>function(a,b) { return priority_order.indexOf(a) - priority_order.indexOf(b); }</code>.</p>
  
  <p>I use the code, which looks like this:</p>
  
  <pre><code>var S6_order = ['city 20 000 - 49 999 citizens', 
            'city 50 000 – 99 999 citizens', 
            'city 100 000 – 199 999 citizens', 
            'city 200 000 – 499 999 citizens', 
            'city above 500 000 citizens'];
  
  var S6_sort = d3.nest()
      .key(function(d) { return d.S6; })
      .sortKeys(function(a,b) { return S6_order.indexOf(a) - S6_order.indexOf(b); })
      .rollup(function(leaves) { return leaves.length; })
      .entries(data);
  </code></pre>
  
  <p>A piece of data I use:</p>
  
  <pre><code>S2,S6,P35,P2_1,P2_2,P2_3,P2_4,P2_5
  2-9 persons,city 20 000 - 49 999 citizens,I like very much,too less,too much,too much,too less,just right
  10-49 persons,city 100 000 – 199 999 citizens,I like very much,too much,I don't know,I don't know,I don't know,I don't know
  2-9 persons,city 200 000 – 499 999 citizens,I like very much,accurately,too less,too less,too less,just right
  10-49 persons,city 50 000 – 99 999 citizens,I do not like neither dislike,too less,too much,too much,too less,too less
  10-49 persons,city above 500 000 citizens,I rather like,too less,accurately,accurately,accurately,just right
  only me,city above 500 000 citizens,I rather like,too less,too less,accurately,accurately,too less
  2-9 persons,city 200 000 – 499 999 citizens,I like very much,accurately,accurately,accurately,accurately,just right
  only me,city 50 000 – 99 999 citizens,I rather like,accurately,accurately,accurately,accurately,just right
  50-249 persons,city 100 000 – 199 999 citizens,I like very much,accurately,too less,too less,accurately,just right
  10-49 persons,city 100 000 – 199 999 citizens,I rather like,too less,accurately,accurately,accurately,just right
  2-9 persons,city 100 000 – 199 999 citizens,I rather like,accurately,accurately,too less,accurately,just right
  2-9 persons,city 50 000 – 99 999 citizens,I rather like,I don't know,accurately,accurately,too less,just right
  only me,city 20 000 - 49 999 citizens,I like very much,accurately,accurately,accurately,I don't know,too much
  2-9 persons,city above 500 000 citizens,I like very much,too less,accurately,too much,too less,too less
  10-49 persons,city above 500 000 citizens,I like very much,accurately,accurately,accurately,accurately,too less
  2-9 persons,city 200 000 – 499 999 citizens,I rather like,accurately,accurately,accurately,accurately,just right
  2-9 persons,city 100 000 – 199 999 citizens,I like very much,accurately,accurately,accurately,accurately,just right
  10-49 persons,city 50 000 – 99 999 citizens,I do not like neither dislike,accurately,accurately,accurately,accurately,just right
  2-9 persons,city 50 000 – 99 999 citizens,I like very much,accurately,accurately,accurately,accurately,just right
  2-9 persons,city 100 000 – 199 999 citizens,raczej go nie lubię,too less,too much,too much,I don't know,I don't know
  2-9 persons,city above 500 000 citizens,I like very much,too less,accurately,accurately,accurately,too less
  2-9 persons,city 50 000 – 99 999 citizens,I rather like,too much,accurately,accurately,accurately,just right
  10-49 persons,city above 500 000 citizens,I do not like neither dislike,too much,accurately,too much,accurately,I don't know
  50-249 persons,city 200 000 – 499 999 citizens,I rather like,I don't know,accurately,accurately,too less,just right
  2-9 persons,city above 500 000 citizens,I rather like,too much,accurately,accurately,too less,just right
  2-9 persons,city 200 000 – 499 999 citizens,I rather like,accurately,accurately,accurately,accurately,just right
  10-49 persons,city above 500 000 citizens,I rather like,too less,too less,accurately,I don't know,too less
  2-9 persons,city 200 000 – 499 999 citizens,I do not like neither dislike,I don't know,I don't know,too much,accurately,I don't know
  10-49 persons,city 200 000 – 499 999 citizens,I rather like,too much,accurately,too much,accurately,just right
  </code></pre>
  
  <p>I load these with:</p>
  
  <pre><code>d3.csv("data.csv", function(file) {
      data = file.map(function(d) {
          return {
              "S2": d.S2,
              "S6": d.S6,
              "P2_1": d.P2_1,
              "P2_2": d.P2_2,
              "P2_3": d.P2_3,
              "P2_4": d.P2_4,
              "P2_5": d.P2_5
              };
          });    
      });
  </code></pre>