0

Here the code is only sorting the li items in ascending order.How to sort in both ascending and descending order by using this code Sorting <li> tags

 function sort(list, key) {
         $($(list).get().reverse()).each(function (outer) {
             var sorting = this;
             $($(list).get().reverse()).each(function (inner) {
                 if ($(key, this).text().localeCompare($(key, sorting).text()) > 0) {
                     this.parentNode.insertBefore(sorting.parentNode.removeChild(sorting), this);
                 }
             });
         });
     }

and

window.onload = function () {
         var desc = false;
         document.getElementById("stCodeDSC").onclick = function () {
             //             sortUnorderedList("list3", desc);

             sort('ul.conn1>li', 'td.code1');
             desc = !desc;
             return false;
         }

Any suggestion?

EDIT: If i give <0 then it is sorting in descending order..

EDIT2:

$(document).ready(function() {
         var desc = false;
         document.getElementById("stCodeDSC").onclick = function () {
             //             sortUnorderedList("list3", desc);

             sort('ul.conn>li', 'td.code');

             desc = !desc;
             return false;
         }
         document.getElementById("stNameDSC").onclick = function () {
             //             sortUnorderedList("list3", desc);

             sort('ul.conn>li', 'td.name');
             desc = !desc;
             return false;
         }
     });
4
  • FYI: When you have jQuery, you do never use window.onload. Commented Mar 26, 2012 at 11:26
  • may i know the reason for that?? Commented Mar 26, 2012 at 11:27
  • Because jQuery provides an easy way to attach an event handler to the load event that does not require overwriting the global one. api.jquery.com/ready Commented Mar 26, 2012 at 11:48
  • When you have jQuery, you also do not use onclick or any of the other "native" onXYZ event handlers. I suggest you spend a little time reading about jQuery, because that's not how it is supposed to be used. Commented Mar 26, 2012 at 14:05

1 Answer 1

1

How about a jQuery plugin:

function elemLocaleCompare(left, right) {
    return $(left).text().localeCompare($(right).text());
}

$.fn.extend({
    sortList: function(order) {
        return this.each(function() {
            var $list = $(this),
                li = $list.children("li").toArray().sort(elemLocaleCompare);

            if (li.length == 0) return true;

            order = order || $list.data("nextOrder") || "asc";

            if (order == "asc")
                $list.append(li).data("nextOrder", "desc");
            else
                $list.append(li.reverse()).data("nextOrder", "asc");
        });
    }
});

Use like:

$("ol").sortList("asc");

// or...
$("#someList").sortList();  // initial sort order, "asc" is default

$("#someButton").click(function () {
  $("#someList").sortList();     // no argument -> toggle sort order
});
​
Sign up to request clarification or add additional context in comments.

4 Comments

<td class="code" style="width: 50%;"> <%# Eval("code") %> </td> <td class="name" style="width: 50%;"> <%# Eval("Name") %> </td>
Here i need to sort based on code in a button click and name in another button click
look at my EDIT2..I need like that
@bala3569 I somehow thought you wanted to sort list items. You might want to use the jQuery tablesorter plugin for your problem. There is no use in re-inventing the wheel for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.