我有时间表。当我使用JavaScript通过单击标题对列进行排序时,它会以不寻常的方式对时间进行排序。我需要根据以1:00:00 pm
到12:00:00 am
开头的数字对时间进行排序。
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
//Set the sorting direction to ascending:
dir = "asc";
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.getElementsByTagName("TR");
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/*If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again.*/
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
&#13;
<table id="myTable" class="table table-striped table-hover table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th onclick="sortTable(0)" width="2%">T.No</th>
<th onclick="sortTable(1)" width="2%">Bth</th>
<th onclick="sortTable(2)" width="45%">Name</th>
<th onclick="sortTable(3)"> In Time</th>
<th>Undo/Delete</th>
</tr>
</thead>
<tbody>
<tr>
<td >1</td>
<td><font color="#E91E63">A</font></td>
<td >aaaaaa</td>
<td >12:01:50 AM</td>
<td>
<a href="goingmrng.php?undoID=65" class="btn btn-sm btn-success" ><i class="fa fa fa-undo"></i></a>
<a href="goingmrng.php?removeIdS=65" class="btn btn-sm btn-danger" ><i class="fa fa-times"></i></a>
</tr>
<tr>
<td >2</td>
<td><font color="#E91E63">A</font></td>
<td >ewwewewer</td>
<td >12:01:57 AM</td>
<td>
<a href="goingmrng.php?undoID=64" class="btn btn-sm btn-success" ><i class="fa fa fa-undo"></i></a>
<a href="goingmrng.php?removeIdS=64" class="btn btn-sm btn-danger" ><i class="fa fa-times"></i></a>
</tr>
<tr>
<td>3</td>
<td><font color="#E91E63">A</font></td>
<td >str</td>
<td >01:00:33 PM</td>
<td>
<a href="goingmrng.php?undoID=63" class="btn btn-sm btn-success" ><i class="fa fa fa-undo"></i></a>
<a href="goingmrng.php?removeIdS=63" class="btn btn-sm btn-danger" ><i class="fa fa-times"></i></a>
</tr>
<tr>
<td>4</td>
<td><font color="#E91E63">A</font></td>
<td >str</td>
<td >01:00:31 PM</td>
<td>
<a href="goingmrng.php?undoID=62" class="btn btn-sm btn-success" ><i class="fa fa fa-undo"></i></a>
<a href="goingmrng.php?removeIdS=62" class="btn btn-sm btn-danger" ><i class="fa fa-times"></i></a>
</tr>
<tr>
<td>5</td>
<td><font color="#E91E63">A</font></td>
<td >sdfasdi</td>
<td >12:01:59 AM</td>
<td>
<a href="goingmrng.php?undoID=61" class="btn btn-sm btn-success" ><i class="fa fa fa-undo"></i></a>
<a href="goingmrng.php?removeIdS=61" class="btn btn-sm btn-danger" ><i class="fa fa-times"></i></a>
</tr>
<tr>
<td>6</td>
<td><font color="#E91E63">A</font></td>
<td >w;e</td>
<td >12:01:59 AM</td>
<td>
<a href="goingmrng.php?undoID=60" class="btn btn-sm btn-success" ><i class="fa fa fa-undo"></i></a>
<a href="goingmrng.php?removeIdS=60" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></a>
</td>
</tr>
</table>
&#13;