我有一个表格中的几个列,比如A、B、C、D和E,我需要在我的HTML页面中显示它们。在一些页面中,我需要仅基于一页的一个列(例如表格的第3列"C")显示排序结果。我可以使用以下代码实现:
function Ascending(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
var rows = $('#table tr').not(':first').get();
$('#table tr').slice(1).remove();
rows.sort(function(rowA, rowB) {
var keyA = $(rowA).children('td').eq(2).text().toUpperCase();
var keyB = $(rowB).children('td').eq(2).text().toUpperCase();
return Ascending(keyA, keyB);
});
但是我还有另一个要求,需要根据两列排序结果来显示结果,即基于上述情况中列C的排序,列E的结果也应该被排序。例如:
排序前:
Column C Column E
2 Fish
1 Box
7 Cat
2 Dog
1 Apple
2 Box
2 Axe
7 Box
2 Answer
7 Apple
6 Year
2 Goat
仅对C列进行排序后:
Column C Column E
1 Box
1 Apple
2 Dog
2 Fish
2 Box
2 Axe
2 Goat
2 Answer
6 Year
7 Box
7 Apple
7 Cat
在对C列和E列进行排序后:
Column C Column E
1 Apple
1 Box
2 Answer
2 Axe
2 Box
2 Dog
2 Fish
2 Goat
6 Year
7 Apple
7 Box
7 Cat
我无法实现它。该怎么做呢?