-1

Is it possible to add an option: "sort list to original order" (first click sort in ascending order, the second click in descending order, then the third click would return to original order)?

If it's possible, you could show how?

this function:

List HTML:

<span style="font-size:140%"> <a id="Sort" href="#">Orden List</a></span><span id="set_order"> ▼</span><br /><br />
<b class="sidebar" id="PostList12">
<li>C</li>
<li>A</li>
<li>D</li>
<li>B</li></b>

Function:

window.onload = function () {
    var desc = false;
    document.getElementById("Sort").onclick = function () {
        sortUnorderedList("PostList12", desc);
        desc = !desc;
        return false;
    }

Function for fix sort function:

function compareText(a1, a2) {
        var t1 = a1.textContent;
        var t2 = a2.textContent;
        return (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
    }

Get List for sort:

function sortUnorderedList(ul, sortDescending) {
    if (typeof ul == "string") {
        ul = document.getElementById(ul);
    }
    var lis = ul.getElementsByTagName("li");
    var vals = [];
    for (var i = 0, l = lis.length; i < l; i++) {
        vals.push(lis[i]);
    }

List alphabetical decrescent:

if (sortDescending) {
            vals.reverse();
            document.getElementById("set_order").innerHTML=" ▼";
        }

List alphabetical crescent:

else {
            vals.sort(compareText);
            document.getElementById("set_order").innerHTML=" ▲";
        }
        for (var i = 0, l = vals.length; i < l; i++) {
            ul.appendChild(vals[i]);
        }
    }
}
jmsmarcelo
  • 95
  • 2
  • 11
  • yes. Is there another question? – mico Jan 01 '16 at 16:50
  • @mico, please, you could show how? – jmsmarcelo Jan 01 '16 at 16:55
  • I meant it is not clear what is wrong, and only general thing everything is possible can be said. please edit and tell what exactly is wrong. That is also why you got down votes from others. – mico Jan 01 '16 at 16:57
  • @mico, I wanted to add one more option, first click sort in ascending order, the second click in descending order, then the third click would return to original orden – jmsmarcelo Jan 01 '16 at 17:04
  • Somehow you have to have a sort function to sort to the original form, or then save it as a separate list in variable. Then maybe have some i you append every press one up and with % operator divide by 3 and then deside what 0, 1 and 2 cases do. – mico Jan 01 '16 at 17:30

2 Answers2

0

This question is partially answered in Undo sort on sorted array in javascript where one good solution is to store the original order to an variable and show it every third click.

There I would introduce variable i which you append with one every click. Then on every click you have three ifs:

if (i % 3 === 0) {
  do first thing
} else if (i % 3 === 1) {
  do second thing
else {
  do third thing.
}

Updating variable on every click keeps the machine rolling and the % operator keeps track of the state action.

In order to get the first action with 0 you have to start from zero and make ifs before the add.

Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99
0

I'm add option set original order list :

window.onload = (function() {

         //this to store the initial order

        var desc = 0;
            var uls = document.getElementById('PostList12');
            var liss = uls.getElementsByTagName("li");
            var valss = [];
            for (var is = 0, ls = liss.length; is < ls; is++) {
                valss.push(liss[is]);
            }

        document.getElementById("Sort").onclick = function () {
            sortUnorderedList("PostList12", desc);
            desc = descOrder(desc);
            return false;
        }


    function descOrder(val) {
        return (val == 0) ? 1 : (val == 1) ? 2 : (val == 2) ? 0 : 1;
    }
    function compareText(a1, a2) {
        var t1 = a1.textContent;
        var t2 = a2.textContent;
        return (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
    }
    function sortUnorderedList(ul, sortDescending) {
        if (typeof ul == "string") {
            ul = document.getElementById(ul);
        }
        var lis = ul.getElementsByTagName("li");
        var vals = [];
        for (var i = 0, l = lis.length; i < l; i++) {
            vals.push(lis[i]);
        }
        if (sortDescending == 1) {
            vals.reverse();
            document.getElementById("set_order").innerHTML=" ◄";
        }
        else if (sortDescending == 2) {
            vals = valss;
            document.getElementById("set_order").innerHTML=" ▼";
        }
        else {
            vals.sort(compareText);
            document.getElementById("set_order").innerHTML=" ▲";
        }
        for (var i = 0, l = vals.length; i < l; i++) {
            ul.appendChild(vals[i]);
        }
    }
});

I'm add all in window.onload for function sortUnorderedList() get the value of valss.

Example Here

jmsmarcelo
  • 95
  • 2
  • 11