How to order (sort) a
  • list with numeric content?
  • 后端 未结 6 2051
    一个人的身影
    一个人的身影 2020-12-19 05:14

    I have a list as below that is created dynamically:

    6条回答
    •  自闭症患者
      2020-12-19 05:30

      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);
      }
      
      
      • 20
      • 10
      • 5
      • 3
      • 32
      • 25
      • 6
      • 12
      • 8

    提交回复
    热议问题