Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,

How do I sort a Java script list from a database?

Regards

What I have tried:

<button onclick="sortList()">Sort</button>
    <ul id="id01">
    <asp:Repeater ID="rptTag" runat="server">
        <ItemTemplate>
            <li>
                <uc1:UCtag runat="server" id="UCtag" />
            </li>
        </ItemTemplate>
    </asp:Repeater>
    </ul>



    <script>
        function sortList() {
            var list, i, switching, b, shouldSwitch;
            list = document.getElementById("id01");
            switching = true;
            /* Make a loop that will continue until
            no switching has been done: */
            while (switching) {
                // start by saying: no switching is done:
                switching = false;
                b = list.getElementsByTagName("LI");
                // Loop through all list-items:
                for (i = 0; i < (b.length - 1); i++) {
                    // start by saying there should be no switching:
                    shouldSwitch = false;
                    /* check if the next item should
                    switch place with the current item: */
                    if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
                        /* if next item is alphabetically
                        lower than current item, mark as a switch
                        and break the loop: */
                        shouldSwitch = true;
                        break;
                    }
                }
                if (shouldSwitch) {
                    /* If a switch has been marked, make the switch
                    and mark the switch as done: */
                    b[i].parentNode.insertBefore(b[i + 1], b[i]);
                    switching = true;
                }
            }
        }
    </script>
Posted
Updated 23-Jun-20 19:50pm
v2
Comments
ZurdoDev 23-Jun-20 16:01pm
   
Where are you stuck?
Member 14683862 23-Jun-20 16:19pm
   
When I press the sort button it does not respond
ZurdoDev 23-Jun-20 16:32pm
   
Use your developer tools and put a breakpoint on the first line of your function. Then run it and click the button. You can then walk through line by line and see what exactly is happening.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The button click will instigate a form post that you need to prevent

<button onclick="return sortList();">Sort</button>


function sortList() {

    // existing code here

    return false;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100