By the way, pay attention to the innerText and innerHTML
Copy codeThe Code is as follows:
Function sortCells (event ){
Var obj = event.tar get;
Var count = 0; count indicates the number of clicks, in ascending or descending order based on parity.
If (! Obj. getAttribute ("clickCount ")){
Obj. setAttribute ("clickCount", 0 );
} Else {
Count = parseInt (obj. getAttribute ("clickCount "));
Count ++;
Obj. setAttribute ("clickCount", count );
}
Var table = event.tar get. parentNode. parentNode;
If (table. nodeName. localeCompare ("THEAD") = 0 ){
If (table. parentNode. nodeName. localeCompare ("TABLE") = 0 ){
Table = table. parentNode;
} Else {
Return;
}
} Else if (table. nodeName. localeCompare ("TBODY") = 0 ){
If (table. parentNode. nodeName. localeCompare ("TABLE") = 0 ){
Table = table. parentNode;
} Else {
Return;
}
} Else if (table. nodeName. localeCompare ("TABLE") = 0 ){
} Else {
Return;
}
Var colNum;
For (x = 0; x <table. rows (1). cells. length; x ++ ){
If(event.tar get. innerText. localeCompare (table. rows (0). cells [x]. innerText) = 0 ){
ColNum = x;
Break;
}
}
Var column = table. rows (1). cells. length;
Var row = table. rows. length;
Var Ar = new Array (row-1 );
For (x = 0; x <row-1; x ++ ){
Ar [x] = new Array (column );
}
For (x = 1; x <row; x ++ ){
For (y = 0; y <column; y ++ ){
Ar [x-1] [y] = table. rows (x). cells (y). innerHTML;
}
}
// Local sorting of strings
/* If (count % 2) = 0 ){
Ar. sort (function (a, B ){
Return B [colNum]. localeCompare (a [colNum])
});
} Else {
Ar. sort (function (a, B ){
Return a [colNum]. localeCompare (B [colNum])
});
}*/
Var temp;
For (x = 0; x <row-1; x ++ ){
For (y = 1; y <row-1; y ++ ){
Temp = Ar [y-1];
If (count % 2) = 0 ){
If (parseInt (Ar [y-1] [colNum])> = parseInt (Ar [y] [colNum]) {
Ar [y-1] = Ar [y];
Ar [y] = temp;
}
} Else {
If (parseInt (Ar [y-1] [colNum]) <= parseInt (Ar [y] [colNum]) {
Ar [y-1] = Ar [y];
Ar [y] = temp;
}
}
}
}
For (x = 1; x <row; x ++ ){
For (y = 0; y <column; y ++ ){
Table. rows (x). cells (y). innerHTML = Ar [x-1] [y];
}
}
Count ++;
}