• beta
  • gamma
  • alpha​​​​​ One of my empl...">

    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
    source share
    2 answers

    $(".uol li") this gives all li elements in the .uol class. .sort(asc) sorts in ascending order. .appendTo('.uol') . Adds the result to the .uol container.

    +2
    source

    . 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
    source

    Source: https://habr.com/ru/post/1444382/


    All Articles