Soni
Soni

Reputation: 425

Sorting in jquery

How to implement sorting in jquery . In Below example "Var List" having html code where i need to implement sorting on click on one filed "#points" . This is working fine in firefox but not in IE and crome . Please suggest some alternative

 $("#points").click(function () {          
        var list = $('#categoryDetailedView>li');
        var sortcol = ".points";
        if ($(this).attr("data-sort") == "desc") {
            list.sort(function (a, b) {
                alert($(sortcol, a).html());
                return parseInt($(sortcol, a).html(), 10) > parseInt($('.points', b).html(), 10);
            });
            $(this).attr("data-sort", "asc");
        } else {
            list.sort(function (a, b) {
                return parseInt($(sortcol, a).html(), 10) < parseInt($('.points', b).html(), 10);
            });
            $(this).attr("data-sort", "desc");
        }

        $('#categoryDetailedView').html(list);
    });

Upvotes: 0

Views: 115

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

In your case list is a jQuery object, not a array, so the sort method won't be there. Try

$("#points").click(function () {
    var list = $('#categoryDetailedView>li').get();
    var sortcol = ".points";
    if ($(this).attr("data-sort") == "desc") {
        list.sort(function (a, b) {
            return parseInt($(sortcol, a).html(), 10) > parseInt($('.points', b).html(), 10) ? 1 : -1;
        });
        $(this).attr("data-sort", "asc");
    } else {
        list.sort(function (a, b) {
            return parseInt($(sortcol, a).html(), 10) < parseInt($('.points', b).html(), 10) ? 1 : -1;
        });
        $(this).attr("data-sort", "desc");
    }

    $('#categoryDetailedView').append(list);
});

Demo: Fiddle

Upvotes: 2

Related Questions