Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a problem in your <code>checkSortValues</code> function where once the data type is set to number, it then tests positive for date also. </p> <p>Also, you have an error in your IsNumeric test, where the <code>input.length</code> will return undefined. You should comment out/change the second part of the test.</p> <pre><code>function IsNumeric(input) { return (input - 0) == input // &amp;&amp; input.length &gt; 0; } </code></pre> <p>If you change the <code>IsNumeric</code> test and if you use <code>else if</code> instead of <code>if</code> for the date test, I think it solves your issue.</p> <pre><code>function checkSortValues(a, b) { var dataType = 'Text'; if ((IsNumeric(a) &amp;&amp; IsNumeric(b)) || (a == null &amp;&amp; IsNumeric(b)) || (IsNumeric(a) &amp;&amp; b == null)) { dataType = 'Numeric'; } else if ((IsDate(a) &amp;&amp; IsDate(b)) || (a == null &amp;&amp; IsDate(b)) || (IsDate(a) &amp;&amp; b == null)) { dataType = 'Date'; } return dataType; } </code></pre> <p>Edit: Include full code with my suggested modifications...</p> <p>I have tested this, and it works for me, sorting in the correct order.</p> <pre><code>&lt;table border=1&gt; &lt;tr style="font-weight:bold;"&gt; &lt;td&gt;Begin Text&lt;/td&gt; &lt;td&gt;Begin Number&lt;/td&gt; &lt;td&gt;Sorted Text&lt;/td&gt; &lt;td&gt;Sorted Number&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r1c1"&gt;&lt;/td&gt; &lt;td id="r1c2"&gt;&lt;/td&gt; &lt;td id="r1c3"&gt;&lt;/td&gt; &lt;td id="r1c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r2c1"&gt;&lt;/td&gt; &lt;td id="r2c2"&gt;&lt;/td&gt; &lt;td id="r2c3"&gt;&lt;/td&gt; &lt;td id="r2c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r3c1"&gt;&lt;/td&gt; &lt;td id="r3c2"&gt;&lt;/td&gt; &lt;td id="r3c3"&gt;&lt;/td&gt; &lt;td id="r3c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r4c1"&gt;&lt;/td&gt; &lt;td id="r4c2"&gt;&lt;/td&gt; &lt;td id="r4c3"&gt;&lt;/td&gt; &lt;td id="r4c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r5c1"&gt;&lt;/td&gt; &lt;td id="r5c2"&gt;&lt;/td&gt; &lt;td id="r5c3"&gt;&lt;/td&gt; &lt;td id="r5c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r6c1"&gt;&lt;/td&gt; &lt;td id="r6c2"&gt;&lt;/td&gt; &lt;td id="r6c3"&gt;&lt;/td&gt; &lt;td id="r6c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r7c1"&gt;&lt;/td&gt; &lt;td id="r7c2"&gt;&lt;/td&gt; &lt;td id="r7c3"&gt;&lt;/td&gt; &lt;td id="r7c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="r8c1"&gt;&lt;/td&gt; &lt;td id="r8c2"&gt;&lt;/td&gt; &lt;td id="r8c3"&gt;&lt;/td&gt; &lt;td id="r8c4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="button" onclick="testSort(1);" value="Sort"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="button" onclick="testSort(2);" value="Sort"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;script&gt; function testSort(orderCol) { orderList = [orderCol]; dataArr = do2DArraySort(dataArr, orderList, 'desc'); for (x=1; x&lt;=numRows; x++) { document.getElementById('r' + x + 'c3').innerHTML = dataArr[x-1][1]; document.getElementById('r' + x + 'c4').innerHTML = dataArr[x-1][2]; } } function TwoDimensionalArray(iRows, iCols) { var i; var j; var a = new Array(iRows); for (i=0; i &lt; iRows; i++) { a[i] = new Array(iCols); for (j=0; j &lt; iCols; j++) { a[i][j] = ""; } } return(a); } function do2DArraySort(dataArr, orderList, orderDir) { //Loop over each item in the list of sort columns. For each one invoke the sort method on the array using the appropriate function. for (x=orderList.length-1; x &gt;= 0; x--) { if (orderDir[x] == 'asc') { dataArr.sort(sortMethodFunctionAsc); } else { dataArr.sort(sortMethodFunctionDesc); } } return dataArr; } function checkSortValues(a, b) { var dataType = 'Text'; if ((IsNumeric(a) &amp;&amp; IsNumeric(b)) || (a == null &amp;&amp; IsNumeric(b)) || (IsNumeric(a) &amp;&amp; b == null)) { dataType = 'Numeric'; } else if ((IsDate(a) &amp;&amp; IsDate(b)) || (a == null &amp;&amp; IsDate(b)) || (IsDate(a) &amp;&amp; b == null)) { dataType = 'Date'; } return dataType; } function sortMethodFunctionAsc(a, b) { if (checkSortValues(a[orderList[x]], b[orderList[x]]) == 'Numeric') { //If the values are numeric, simply check which is larger than the other. return a[orderList[x]] - b[orderList[x]]; } else if (checkSortValues(a[orderList[x]], b[orderList[x]]) == 'Date') { //If the values are dates they need converted to date objects. 95% of the time this is not necessary as they are already passed in as dates, //but the conversion is required to catch the few cases when they are not. var a2 = new Date(a[orderList[x]]); var b2 = new Date(b[orderList[x]]); //The getTime method is used to convert the dates into millisecond ticker equivalents for easier comparison. return a2.getTime() - b2.getTime(); } else { //If one of the values is a string, convert both to a string and compare alphabetically. if (a[orderList[x]].toString() &gt; b[orderList[x]].toString()) { return 1; } else if (a[orderList[x]].toString() &lt; b[orderList[x]].toString()) { return -1; } else { //If they are the same, tell the sort to skip them. return 0; } } } function sortMethodFunctionDesc(a, b) { //This function is identical to the ascending one, but the comparison operators are reversed. if (checkSortValues(a[orderList[x]], b[orderList[x]]) == 'Numeric') { return b[orderList[x]] - a[orderList[x]]; } else if (checkSortValues(a[orderList[x]], b[orderList[x]]) == 'Date') { var a2 = new Date(a[orderList[x]]); var b2 = new Date(b[orderList[x]]); return b2.getTime() - a2.getTime(); } else { if (a[orderList[x]].toString() &lt; b[orderList[x]].toString()) { return 1; } else if (a[orderList[x]].toString() &gt; b[orderList[x]].toString()) { return -1; } else { return 0; } } } function IsNumeric(input) { return (input - 0) == input// &amp;&amp; input.length &gt; 0; } function IsDate(testValue) { var returnValue = false; var testDate; try { testDate = new Date(testValue); if (!isNaN(testDate)) { returnValue = true; } else { returnValue = false; } } catch (e) { returnValue = false; } return returnValue; } numRows = 8; orderList = [1]; dataArr = TwoDimensionalArray(numRows, 2); dataArr[0][1] = 'Jimbo'; dataArr[0][2] = 3; dataArr[1][1] = 'Jim'; dataArr[1][2] = 0.65; dataArr[2][1] = 'Jackie'; dataArr[2][2] = 1.25; dataArr[3][1] = 'John'; dataArr[3][2] = 0.8; dataArr[4][1] = 'Jacob'; dataArr[4][2] = 0.95; dataArr[5][1] = 'Jill'; dataArr[5][2] = 0.85; dataArr[6][1] = 'Jan'; dataArr[6][2] = 0.8; dataArr[7][1] = 'Jamie'; dataArr[7][2] = 1.45; for (x=1; x&lt;=numRows; x++) { document.getElementById('r' + x + 'c1').innerHTML = dataArr[x-1][1]; document.getElementById('r' + x + 'c2').innerHTML = dataArr[x-1][2]; } &lt;/script&gt; </code></pre> <p>You can confirm that <a href="http://jsfiddle.net/Sq62T/1/" rel="nofollow">here</a> if you want to...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload