I'm trying to take the contents of a list, that is not in alphabetical order, then by adding each item to an array, sort them into alphabetical order and insert them back into the list in alphabetical order.
Plain language: I need to alphabetize a list using JS or jQuery.
Here's what I have. I just can't figure out how to insert the contents of the array in back into the list.
Thank you all in advance :)
var sectors = [];
$("li").each(function() { sectors.push($(this).text()) });
sectors.sort();
console.log(sectors);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<ul>
<li id="alphabet">B</li>
<li id="alphabet">C</li>
<li id="alphabet">A</li>
</ul>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…