I\'m a bit out of my depth here and I\'m hoping this is actually possible.
I\'d like to be able to call a function that would sort all the items in my list alphabeti
improvement based on Jeetendra Chauhan's answer
$('ul.menu').each(function(){
$(this).children('li').sort((a,b)=>a.innerText.localeCompare(b.innerText)).appendTo(this);
});
why i consider it an improvement:
using each
to support running on more than one ul
using children('li')
instead of ('ul li')
is important because we only want to process direct children and not descendants
using the arrow function (a,b)=>
just looks better (IE not supported)
using vanilla innerText
instead of $(a).text()
for speed improvement
using vanilla localeCompare
improves speed in case of equal elements (rare in real life usage)
using appendTo(this)
instead of using another selector will make sure that even if the selector catches more than one ul still nothing breaks