title: Toggle array sort in Javascript (ascending to descending and vice versa)
body: <p>I am sorting my array like this:</p>
  
  <pre><code>array.sort((function(index) {
      return function(a, b){
          return (a[index] === b[index] ? 0 : (a[index] &lt; b[index] ? -1 :1));
      };
  })(0));
  </code></pre>
  
  <p>As you can see, it is sorted in ascending order.</p>
  
  <p>My question is how do I toggle sorting? For example, if it is already in ascending order then how can I sort it in descending order and vice-versa?</p>
  
  <p>I know to sort in descending I need to modify code like this:</p>
  
  <pre><code>array.sort((function(index) {
      return function(a, b) {
          return (a[index] === b[index] ? 0 : (a[index] &lt; b[index] ? 1 :-1));
      };
  })(0));
  </code></pre>
  
  <p>but I don't know how to toggle.</p>