MonstaMash
MonstaMash

Reputation: 67

Sorting a sortable list

Im using Jquery UI to get users to sort a list. I have a button to show answers if they want, which will put the list in order, based on ID. The function that sorts the list is:

function show_order() {

    var elems = $('#sortable').children('li').remove();
    elems.sort(function(a,b){
        return parseInt(a.id) > parseInt(b.id);
    });
    $('#sortable').append(elems);
}

This works fine in Chrome and Firefox, but not in IE (11) - with no error in console.

See http://jsfiddle.net/bvacK/ for example.

Upvotes: 3

Views: 107

Answers (1)

David Hedlund
David Hedlund

Reputation: 129832

.sort expects you to return a value less than 0 (a less than b), 0 (equal) or a value greater than 0 (a greater than b). Simply returning the result of a > comparison will yield a boolean. Change your code accordingly:

return parseInt(a.id, 10) - parseInt(b.id, 10);

Upvotes: 3

Related Questions