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:
- Get the contents of each table cell having the class
text
. For each cell:- Split it by
(
- Get the first item from the array returned by
.split()
, and push it tonames1
- Loop through each table row:
- Get the row's contents
- Split again, while grabbing the returned array's second item
- Compare it to the value of the corresponding item from
names1
- Split it by
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>