jquery - Having trouble with javascript sort() order -


this html code put in array sort. trying numbers sort in order lowest highest (in ids). code seems resort top bottom while maintaining current order. having looked around , trying few different methods, have not found solution of yet. suggestions?

<a href="#" class="link-sort-list asc">a-z</a> <a href="#" class="link-sort-list desc">z-a</a> <ul id="sortable"> <li id="220" class="p_box"></li> <li id="221" class="p_box"></li> <li id="217" class="p_box"></li> <li id="215" class="p_box"></li> <li id="219" class="p_box"></li> <li id="216" class="p_box"></li> <li id="218" class="p_box"></li> <li id="214" class="p_box"></li> <li id="208" class="p_box"></li> <li id="206" class="p_box"></li> </ul> 

this javascript sort function. issue might here not sure.

var $list = $('#sortable'); var $myid = []; var $myid = $(".p_box");  //sort click buttons     $('.link-sort-list').click(function(e) {     if ($(this).hasclass('asc')){     $list.empty().append($myid.sort(function(a,b){return < b}));     } else {     $list.empty().append($myid.sort());     }     e.preventdefault();     }); 

you have sort id, , sort other way, reverse condition

var $list = $('#sortable');  $('.link-sort-list').click(function(e) {     e.preventdefault();     if ($(this).hasclass('asc')) {         $list.find('li').sort(function(a, b) {             return a.id - b.id;         }).appendto($list);     } else {         $list.find('li').sort(function(a, b) {             return b.id - a.id;         }).appendto($list);     } }); 

fiddle


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -