javascript - How to sort elements in a list with numbers? -


this question has answer here:

i trying sort list:

<div id="mainmenu">     <span>thing 5</span>     <span>thing 4</span>     <span>thing 7</span>     <span>thing 1</span>     <span>thing 5</span>     <span>thing 6</span>     <span>thing 3</span> </div> 

i have tried:

$('#mainmenu').sort(function(){     $(this).children().sort(); }); 

i want sorted in order:

<span>thing 1</span> <span>thing 2</span> <span>thing 3</span> <span>thing 4</span> <span>thing 5</span> <span>thing 6</span> <span>thing 7</span> 

try this,

$span = $('#mainmenu span').sort(function(a, b) {     if ($(a).text() < $(b).text()) return -1;     if ($(a).text() > $(b).text()) return 1;     return 0; }); $('#mainmenu').html($span); 

demo


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -