title: Toggle a sort function in React
body: <p>I'm created a table that display prices and a function to sort them but i can't figure out how to toggle between ascending and descending. </p>
  
  <pre><code>...//my states
        this.compareBy.bind(this);
        this.sortBy.bind(this);
    };
  
    compareBy(key) {
      return function (a, b) {
        if (a[key] &lt; b[key]) return -1;
        if (a[key] &gt; b[key]) return 1;
        return 0;
      };
    }
  
  
    sortBy(key) {
      let arrayCopy = [...this.state.data];
      arrayCopy.sort(this.compareBy(key));
      this.setState({data: arrayCopy});
    }
  
   &lt;th  onClick={() =&gt; this.sortBy('name')}&gt;Name&lt;/th&gt;
  
  </code></pre>
  
  <p>Here is the function of my code.</p>