title: D3 Sorting Bar Chart Error n.each
body: <p>I have the problem that everytime i try to execute my function sortBars(), I get the console error "Uncaught TypeError: n.each is not a function" in the console. I haven't found anything in relation to my problem.</p>
  
  <p>Heres my JS Code:</p>
  
  <pre><code>  var margin = {top: 20, right: 0, bottom: 70, left: 40},
      width = 600 - margin.left - margin.right,
      height = 800 - margin.top - margin.bottom;
  
  // Parse the date / time
  
  var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);
  
  var y = d3.scale.linear().range([height, 0]);
  
  var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom")
      .tickFormat(d3.name);
  
  var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .ticks(10);
  
  
  var tip = d3.tip()
    .attr('class', 'd3-tip')
    .offset([-10, 0])
    .html(function(d) {
      return "&lt;strong&gt;"+ d.name +"&lt;/strong&gt; &lt;span style='color:red'&gt;" + d.value + "&lt;/span&gt;";
    })
  
  
  var svg = d3.select("body").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform", 
            "translate(" + margin.left + "," + margin.top + ")");
  svg.call(tip);
  
  d3.csv("shit.csv", function(error, data) {
  
      data.forEach(function(d) {
          d.name = d.name;
          d.value = d.value;
      });
  
    x.domain(data.map(function(d) { return d.name; }));
    y.domain([0, d3.max(data, function(d) { return d.value; })]);
  
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis)
      .selectAll("text")
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", "-.55em")
        .attr("transform", "rotate(-90)" );
  
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis)
      .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("text-anchor", "end");
  
    svg.selectAll(".bar")
        .data(data)
      .enter().append("rect")
    .on('mouseover', tip.show)
        .on('mouseout', tip.hide)
    .attr("class", "bar")
        .style("fill", "steelblue")
        .attr("x", function(d) { return x(d.name); })
        .attr("width", x.rangeBand())
        .attr("y", function(d) { return y(d.value); })
        .attr("height", function(d) { return height - y(d.value); });
  });
  
  var sortOrder = false;
  var sortBars = function () {
      sortOrder = !sortOrder;
  
      sortItems = function (a, b) {
          if (sortOrder) {
              return a.value - b.value;
          }
          return b.value - a.value;
      };
  
      svg.selectAll(".bar")
          .sort(sortItems)
          .transition()
          .delay(function (d, i) {
          return i * 50;
      })
          .duration(1000)
          .attr("x", function (d, i) {
          return xAxis(i);
      });
  
  
  };
  
  d3.select("#sort").on("click", sortBars);
  
  function randomSort() {
  
      svg.selectAll(".bar")
          .sort(sortItems)
          .transition()
          .delay(function (d, i) {
          return i * 50;
      })
          .duration(1000)
          .attr("x", function (d, i) {
          return xAxis(i);
      });
  
  }
  </code></pre>