Organizing List Items
I have an unordered list with me
<ul class="uol"> <li>beta</li> <li>gamma</li> <li>alpha</li> </ul>βββββ One of my employees (on a long vacation) wrote code sorting this list.
$(".uol li").sort(asc).appendTo('.uol'); function asc(a, b){ return ($(b).text()) < ($(a).text()); } function desc(a, b){ return ($(b).text()) > ($(a).text()); }β ( fiddle )
I can't figure out what the code does, especially this line:
$(".uol li").sort(asc).appendTo('.uol'); Can someone explain this? Also, is this the best way to sort UOL or is there a better way that you know about?
+4
2 answers
. sort () is a javascript function that sorts an array using the sort function you pass.
the function that he chose to pass checks between two adjacent values ββthat one text has a lower ascii value. the sorter will continue to perform this function between every two adjacent values ββin the array until it is sorted.
0