Sorting
  • tags
  • 前端 未结 4 981
    一向
    一向 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:48

      Mine answer is longer :p but work.

      function SortLIs() {
          var ColumnUL = $("ul.column");
          var Columns  = $(ColumnUL).children("li");
           
          var ColumnNames    = new Array();
          var Columns_byName = new Array();
          var Columns_Count  = Columns.length;
          for(var i = 0; i <  Columns_Count; i++) {
              var aColumn = Columns[i];
              var aTD     = $(aColumn).find(".name");
              var aTDName = aTD.text();
              ColumnNames.push(aTDName);
              Columns_byName[aTDName] = aColumn;
      
              $(aColumn).remove();
          }
           
          ColumnNames.sort(function(a, b){
              return (a >  b) - (a <  b);
          });
           
          for(var i = 0; i <  Columns_Count; i++) {
              var aName = ColumnNames[i];
              ColumnUL.append(Columns_byName[aName]);
          }
      }
      

      EDIT: I saw you said that you are not good at JS. So here is the bigger picture for you.

      (1) Add The following code to the header of the HTML. This will use jQuery library.

      (2) Add the 'sortLIs' code just right after the above code

      
      

      (3.1) If you want the sorting to begin at the load time. Add this right after the above code.

      
      

      (3.2) Otherwise, you call the function from an event.

      Hope this helps.

    提交回复
    热议问题