1 var allNums = false; // boolean variable to see if whole column is numeric2 var allDates = false; // boolean variable to see if column vals are all dates3 var lastSort = -1; // variable to hold last column number sorted4 var absOrder = true; // boolean to sort in reverse if on same column5 //-----------------------------------------------------------------------6 function setDataType(inValue) {7 // This function checks data type of a value8 // - sets allNums to false if a non-number is found9 // - sets allDates to false if a non-date is found10 var isDate = new Date(inValue);11 if (isDate == "NaN") {12 if (isNaN(inValue)){13 // The value is a string, make all characters in14 // String upper case to assure proper a-z sort15 inValue = inValue.toUpperCase();16 allNums = false17 allDates = false18 return inValue;19 }20 else {21 // Value is a number, make sure it is not treated as a string22 allDates = false23 return parseFloat(1*inValue);24 }25 }26 else {27 // Value to sort is a date28 allNums = false29 return inValue ;30 }31 }32 //-----------------------------------------------------------------------33 function sortTable(col){34 if (lastSort == col){35 // sorting on same column twice = reverse sort order36 absOrder ? absOrder = false : absOrder = true37 }38 else{39 absOrder = true40 }41 lastSort = col42 allTR = document.getElementById("dataTable").childNodes[0].childNodes43 // allTR now holds all the rows in the dataTable44 totalRows = allTR.length45 colToSort = new Array() //holds all the cells in the column to sort46 colArr = new Array() //holds all the rows that correspond to the sort cell47 copyArr = new Array() //holds an original copy of the sort data to match to colArr48 resultArr = new Array() //holds the output49 50 allNums = true51 allDates = true52 53 //store the original data54 //remember that the first row - [0] - has column headings55 //so start with the second row - [1]56 //and load the contents of the cell into the array that will be sorted57 for (x=1; x < totalRows; x++){58 colToSort[x-1] = setDataType(allTR[x].childNodes[col].innerText)59 colArr[x-1] = allTR[x]60 }61 //make a copy of the original62 for (x=0; x<colToSort.length; x++){63 copyArr[x] = colToSort[x]64 }65 66 //sort the original data based on data type67 if (allNums){68 colToSort.sort(numberOrder)69 }70 else if (allDates){71 colToSort.sort(dateOrder)72 }73 else{74 colToSort.sort(textOrder)75 }76 77 //match copy to sorted78 for(x=0; x<colToSort.length; x++){79 for(y=0; y<copyArr.length; y++){80 if (colToSort[x] == copyArr[y]){81 boolListed = false82 //searcg the ouput array to make sure not to use duplicate rows83 for(z=0; z<resultArr.length; z++){84 if (resultArr[z]==y){85 boolListed = true86 break;87 }88 }89 if (!boolListed){90 resultArr[x] = y91 break;92 }93 }94 }95 }96 //now display the results - it is as simple as swapping rows97 for (x=0; x<resultArr.length; x++){98 allTR[x+1].swapNode(colArr[resultArr[x]])99 }100 }101 //-----------------------------------------------------------------------102 function numberOrder(a,b){103 absOrder ? rVal = b - a : rVal = a - b104 return rVal105 }106 //-----------------------------------------------------------------------107 function dateOrder(a,b){108 absOrder ? rVal = Date.parse(a) - Date.parse(b) : rVal = Date.parse(b) - Date.parse(a)109 return rVal110 }111 //-----------------------------------------------------------------------112 function textOrder(a,b){113 if (a.toString() < b.toString()){114 absOrder ? rVal = -1 : rVal = 1115 }116 else{117 absOrder ? rVal = 1 : rVal = -1118 }119 return rVal120 }
Enlace
El enlace para compartir es: