KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an HTML structure as follows: <div class="clist"> <div data-sid=1></div> <div data-sid=2></div> <div data-sid=2></div> <div data-sid=1></div> <div data-sid=2></div> <div data-sid=2></div> <div data-sid=1></div> </div> I would like to sort them as: <div class="clist"> <div data-sid=1></div> <div data-sid=1></div> <div data-sid=1></div> <div data-sid=2></div> <div data-sid=2></div> <div data-sid=2></div> <div data-sid=2></div> </div> I am using the following function: function sortContacts() { var contacts = $('div.clist'), cont = contacts.children('div'); cont.detach().sort(function(a, b) { var astts = $(a).data('sid'); var bstts = $(b).data('sid') return (astts > bstts) ? (astts > bstts) ? 1 : 0 : -1; }); contacts.append(cont); } It is not working as expected. It is working well for the first run but after adding new element or changing the data-sid attributes it no longer works. Demo: http://jsfiddle.net/f5mC9/1/ Not working?
Tags (comma-separated)
Save Edits
Cancel