How to order (sort) a
  • list with numeric conte
  • 2020-02-12 04:32发布

    I have a list as below that is created dynamically:

    <ul>
      <li>20</li>
      <li>10</li>
      <li>5</li>
      <li>3</li>
      <li>32</li>
      <li>25</li>
      <li>6</li>
      <li>12</li>
      <li>8</li>
    </ul>

    Is it possible order this list using jQuery? I need it ordered from lower to bigger, like this:

    <ul>
      <li>3</li>
      <li>5</li>
      <li>6</li>
      <li>8</li>
      <li>10</li>
      <li>12</li>
      <li>20</li>
      <li>25</li>
      <li>32</li>
    </ul>

    6条回答
    三岁会撩人
    2楼-- · 2020-02-12 05:08

    Using sortContent plugin ,everything will be easy,clean and ready for reuse :

     $('ul').sortContent({helper:myhelper});
    

    Known that :

    myhelper=function(e){
       return $('a',$(e)).attr('href');
    }
    

    DEMO


    If you want to discover other options , visit Plugin HOMEPAGE

    查看更多
    不美不萌又怎样
    3楼-- · 2020-02-12 05:09
    var li = $('ul li');
    li.sort(function(a, b) {
        if(parseInt($(a).text()) > parseInt($(b).text()))
            return 1;
        else return -1;
    });
    $('ul').empty().html(li);
    
    查看更多
    戒情不戒烟
    4楼-- · 2020-02-12 05:10

    There are jQuery plugins to handle this as well. Check out TinySort

    查看更多
    Fickle 薄情
    5楼-- · 2020-02-12 05:13

    Below should do the trick:

    $(function() {
      $('button').click(function() {
        var liContents = [];
        $('ul li').each(function() {
          liContents.push(parseInt($(this).text(), 10));
        });
        liContents.sort(numOrdDesc);
        $('ul li').each(function() {
          $(this).text(liContents.pop());
        });
      });
    });
    
    function numOrdDesc(a, b) {
      return (b - a);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul>
      <li>20</li>
      <li>10</li>
      <li>5</li>
      <li>3</li>
      <li>32</li>
      <li>25</li>
      <li>6</li>
      <li>12</li>
      <li>8</li>
    </ul>
    
    <button>Sort</button>

    查看更多
    仙女界的扛把子
    6楼-- · 2020-02-12 05:24

    You can do:

    var listItems = [];
    $("#list li").each(function() {
      console.log($(this).text());
      listItems.push(parseInt($(this).text()));
    });
    
    listItems.sort(function(a, b) {
      return a - b
    });
    
    $("#list").html("");
    $.each(listItems, function(i, v) {
      $("#list").append($("<li>" + v + "</li>"));
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul id="list">
      <li>20</li>
      <li>10</li>
      <li>5</li>
      <li>3</li>
      <li>32</li>
      <li>25</li>
      <li>6</li>
      <li>12</li>
      <li>8</li>
    </ul>

    查看更多
    疯言疯语
    7楼-- · 2020-02-12 05:29

    Here is a possible way to do it:

    $(function() {
      var elems = $('ul').children('li').remove();
      elems.sort(function(a, b) {
        return parseInt($(a).text()) > parseInt($(b).text());
      });
      $('ul').append(elems);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul>
      <li>20</li>
      <li>10</li>
      <li>5</li>
      <li>3</li>
      <li>32</li>
      <li>25</li>
      <li>6</li>
      <li>12</li>
      <li>8</li>
    </ul>

    查看更多
    登录 后发表回答