Toggle array sort in Javascript (ascending to descending and vice versa)
<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 that it is sorted in ascending order. My question is how do I toggle sorting? ie if it is already in ascending order, then sort it in descending order and if it is already in descending order then sort it in ascending order. 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 two toggle.</p>
