根据第一个元素对多维数组进行排序

时间:2016-03-23 10:01:35

标签: javascript jquery arrays multidimensional-array

我已根据<td> s&#39;成功对表格行进行了排序。内容(表格单元格为&#39;文本&#39;),所以为了细化细节,我必须:

  1. 获取具有类text的每个表格单元格的内容。对于每个细胞:
    1. (
    2. 拆分
    3. .split()返回的数组中获取第一个项,并将其推送至names1
    4. 遍历每个表格行:
      1. 获取行的内容
      2. 再次拆分,同时抓住返回的阵列第二项目
      3. 将其与names1
      4. 中相应项目的值进行比较
  2. 那么,我的问题是如何按字母顺序排序/排序?

    &#13;
    &#13;
    $(document).ready(function() {
      $("table .text").each(function() {
        var this_current = $(this);
        $(this).addClass("dirty");
        var text = $(this).text().replace('(', '').replace(')', '').replace(' ', '');
        $("table .text:not(.dirty)").each(function() {
          var text2 = $(this).text().replace('(', '').replace(')', '').replace(' ', '');
          if (text >= text2) {
            $(this).closest('tr').prev().prev().prev().before(this_current.closest("tr"));
          } else {
            $(this).closest('tr').after(this_current.closest("tr"));
          }
        });
        $(this).removeClass("dirty");
      });
    });
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    
    <table>
      <tr>
        <td>table cell of the 1st row</td>
      </tr>
      <tr>
        <td>table cell of the 2nd row</td>
      </tr>
      <tr>
        <td>table cell of the 3rd row</td>
      </tr>
      <tr>
        <td class="text">ILIGAN (DALIPUGA)</td>
      </tr>
      <tr>
        <td>table cell of the 5th row</td>
      </tr>
      <tr>
        <td>table cell of the 6th row</td>
      </tr>
      <tr>
        <td>table cell of the 7th row</td>
      </tr>
      <tr>
        <td class="text">ILIGAN (BB)</td>
      </tr>
      <tr>
        <td>table cell of the 9th row</td>
      </tr>
      <tr>
        <td>table cell of the 10th row</td>
      </tr>
      <tr>
        <td>table cell of the 11th row</td>
      </tr>
      <tr>
        <td class="text">ILIGAN (BB)</td>
      </tr>
      <tr>
        <td>table cell of the 13th row</td>
      </tr>
      <tr>
        <td>table cell of the 14th row</td>
      </tr>
      <tr>
        <td>table cell of the 15th row</td>
      </tr>
      <tr>
        <td class="text">CDO (AGUSAN)</td>
      </tr>
      <tr>
        <td>table cell of the 17th row</td>
      </tr>
      <tr>
        <td>table cell of the 18th row</td>
      </tr>
      <tr>
        <td>table cell of the 19th row</td>
      </tr>
      <tr>
        <td class="text">ILIGAN (AA)</td>
      </tr>
      <tr>
        <td>table cell of the 20th row</td>
      </tr>
      <tr>
        <td>table cell of the 21th row</td>
      </tr>
      <tr>
        <td>table cell of the 22th row</td>
      </tr>
      <tr>
        <td class="text">ILIGAN (JJ)</td>
      </tr>
    </table>
    &#13;
    &#13;
    &#13;

0 个答案:

没有答案