jquery - List element not sorting properly -


i wanted sort li elements based on data-order not sorting properly.

    <ul> <li>ten ** having 0<input type="checkbox" name="siz" data-order='0'/></li> <li>ten<input type="checkbox" name="siz" data-order='0'/></li> <li>one <input type="checkbox" data-order='-1' name="one" value="1"/></li> <li>two <input type="checkbox" data-order='-2' name="twp"/></li> <li>threee<input type="checkbox" name="thre" data-order='3'/></li> <li>four<input type="checkbox" name="for" data-order='4'/></li> <li>five<input type="checkbox" data-order='5' name="fibe"/></li> <li>six<input type="checkbox" name="siz" data-order='6'/></li> <li>seven<input type="checkbox" name="siz" data-order='7'/></li> <li>eight<input type="checkbox" name="siz" data-order='8'/></li> <li>nine<input type="checkbox" name="siz" data-order='9'/></li> <li>ten ** having 0<input type="checkbox" name="siz" data-order='0'/></li>  </ul> 
var elems = $('ul li'); elems.sort(function(a, b) {   return $(a).find('input').attr('data-order') < $(b).find('input').attr('data-order') }).appendto(elems.parent()); 

what missing sort list of elements? see fiddle here: https://jsfiddle.net/6d92gy9y/

descending order:

var elems = $('ul li');  elems.sort(function(a, b) {     if ($(a).find('input').attr('data-order') < $(b).find('input').attr('data-order'))     return -1;     if ($(a).find('input').attr('data-order') > $(b).find('input').attr('data-order'))     return 1;     return 0;  }).appendto(elems.parent()); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -