Sort a multidimensional array based on its first element

Viewed 115

I have successfully sorted the table rows based on its <td>s' contents (table cell with a class of 'text'), so to sharpen the details, I have to:

  1. Get the contents of each table cell having the class text. For each cell:
    1. Split it by (
    2. Get the first item from the array returned by .split(), and push it to names1
    3. Loop through each table row:
      1. Get the row's contents
      2. Split again, while grabbing the returned array's second item
      3. Compare it to the value of the corresponding item from names1

So, my question is how to sort/order alphabetically?

$(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");
  });
});
<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>

0 Answers
Related