title: Sorting large CSV in D3.js
body: <p>I'm trying to sort a medium sized CSV file (~3000 lines) using D3.js before passing it to a visualization algorithm. </p>
  
  <p><img src="https://i.stack.imgur.com/4UXPD.png" alt="enter image description here"></p>
  
  <p>Apparently though there's something wrong with my code as the sorting seems to be applied in 'chunks' - see the image above. I'm using this d3 function: </p>
  
  <pre><code>            dataset.sort(function(a,b) {
                return d3.descending(a.counttotal, b.counttotal);
            });
  </code></pre>
  
  <p>Here's the code: </p>
  
  <pre><code>    // Your D3 code will go here
      // safe way to load data 
  
      var dataset = [];
      d3.csv("subjects.csv",
              function(error, rows) {
                  rows.forEach(function(r) {
                      dataset.push({
                          label: r.label,
                          counttotal: r.counttotal
                      })
                  });
                  generateVis();
              });
  
  
      function generateVis(){
  
  
          var h = dataset.length * 100; //7000;
          var w = 1000;
  
  
          dataset.sort(function(a,b) {
                return d3.descending(a.counttotal, b.counttotal);
            });
  
  
          var svg = d3.select("#svg-rect").append("svg")
              .attr('id','mySVG')
              .attr("width", w )
              .attr("height", h);
  
          var rects = svg.selectAll("rect")
                     .data(dataset)
                     .enter()
                     .append("rect");           
  
          rects.attr("x", 0)
              .attr("y", function(d, i){
                  return i * 10 + 10;
              })
              .attr("width", function(d){
                  return d.counttotal;
              })
              .attr("height", 8)
              .append("title")
              .text(function (d){
                  return d.label + " has " + d.counttotal + " articles ";
              })
  
  
      };
  </code></pre>
  
  <p>The CSV file can be found here: <a href="https://www.dropbox.com/s/3kwzwqbgts8z2lh/subjects.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/3kwzwqbgts8z2lh/subjects.csv?dl=0</a></p>
  
  <p>Any help is greatly appreciated!</p>