3

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>
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • As it is right now your question is pretty unclear, and from what I can tell you're working with data attributes that are not included in your sample HTML. – SeinopSys Mar 28 '16 at 00:24
  • @SeinopSys: sorry im doing a test on my snippet (doing my things to solve my own issue). So code might a little bit unconnected. – Juliver Galleto Mar 28 '16 at 00:34
  • If I understand your question correctly, what you should probably do is have an array of objects, and [sort that](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) instead of having 2 separate arrays. – SeinopSys Mar 28 '16 at 00:37
  • @SeinopSys: I have two sub array objects per parent array object e.g. array[0](['sub array 1','sub array 2']), the 'sub array 2', contains integer, and what I want is to sort the objects array per 'sub array 1' where 'sub array 1' contains string and when sorted, both 'sub array 1' and 'sub array 2' should go along together but what actually happen is, yes, the array is sorted as I want, but only the 'sub array 1' is sorted while the 'sub array 2' was not like the 'sub array 2' was indexed base on there integer length. Any ideas – Juliver Galleto Mar 28 '16 at 01:00
  • I tried to reformat the question's wording to be more clear, please check if that is indeed what you intended to write. – SeinopSys Mar 28 '16 at 21:47
  • Have you considered using one of the many data table plug-ins available for jQuery to handle this sort of table sorting functionality for you? – Mike Brant Mar 28 '16 at 21:47
  • @SeinopSys: Thank you for that :) – Juliver Galleto Mar 29 '16 at 00:55
  • @MikeBrant: I usually use datatables but due to certain requirements where my table has no header and also in actual environment, there's a col span and row span included, so I cant use datatables with it. – Juliver Galleto Mar 29 '16 at 00:57

0 Answers0