Sorting
  • tags
  • 前端 未结 4 1092
    一向
    一向 2020-12-16 05:16

    I have a a and I would like to sort my list alphabetically (I don\'t want caps to matter) according to a class named \"name\". How would I do this?

    4条回答
    •  予麋鹿
      予麋鹿 (楼主)
      2020-12-16 05:49

      Here's another approach, stealing ideas from the other answers given so far (also requiring jQuery):

      function sort(elementSelector, valueSelector, ascending) {
        var sign = ascending ? -1 : 1;
        var elements = jQuery(elementSelector);
        elements.each(function() {
          this.sortKey = jQuery(valueSelector, this).text();
        });
        var sorted = elements.get();
        sorted.sort(function(a, b) {
          var keyA = a.sortKey;
          var keyB = b.sortKey;
          return sign * ((keyA < keyB) - (keyA > keyB));
        });
        elements.parent().append(sorted);
      }
      
      sort('.column>li', '.name', true)
      

    提交回复
    热议问题