I've implemented a function that successfully sorts a JSON array when a link is clicked (the function is triggered by an onclick event). Here's the current sorting function in action:
function sortArch(a, b) {
x = eval("a." + sort_type).toLowerCase();
y = eval("b." + sort_type).toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
Now, I want to modify the function so that when the same button is clicked again, it will perform a reverse sort. Reversing the sort order is straightforward:
x = eval("a." + sort_type).toLowerCase();
y = eval("b." + sort_type).toLowerCase();
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
However, my challenge lies in ensuring that the function "knows" which way it needs to sort. I've considered using a boolean flag, but haven't been able to find a solution that actually works. I just need a reliable method to keep track of the current sorting direction and then apply the appropriate sorting logic when the button is clicked for the second time. Appreciate any assistance on this matter!