2017-05-30 53 views
0

我想表与这两个词和数字列进行排序的表格。这些词是按照预期工作的。但数字 - 部分是有点关闭。试图梳理与数字

其中一列是价格列从500-1500金额。但现在,500下降到1500以下,我不知道如何解决这个问题。

function sortTable(n) { 
    var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; 
    table = document.getElementById("allCars"); 
    switching = true; 

    dir = "asc"; 

    while (switching) { 
     switching = false; 
     rows = table.getElementsByTagName("TR"); 
     for (i = 1; i < (rows.length - 1); i++) { 
     shouldSwitch = false; 
     x = rows[i].getElementsByTagName("TD")[n]; 
     y = rows[i + 1].getElementsByTagName("TD")[n]; 
     if (dir == "asc") { 
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { 
      shouldSwitch= true; 
      break; 
     } 
     } else if (dir == "desc") { 
     if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { 
      shouldSwitch= true; 
      break; 
     } 
     } 
    } 
    if (shouldSwitch) { 
     rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); 
     switching = true; 
     switchcount ++; 
    } else { 
     if (switchcount == 0 && dir == "asc") { 
     dir = "desc"; 
     switching = true; 
     } 
    } 
    } 
} 
+0

你的排序是打破了整数,但正在为字符串的原因是因为它的排序整数作为一个字符串,因此,比较1500〜500的问题是,1小于5。这个答案解释比较详细(更好)https://softwareengineering.stackexchange.com/a/127644 也许考虑它定义它的类型表的顶级行(TH)的属性,那么你可以使用parseInt函数解析,你知道的值将是一个int – Robbie

+1

而不是手动执行此操作,dataTables(datatables.net)会更好。但如果你是反对的是,检查它是否是一个数字,然后用数(VAL)的值转换为数字 – Simon

回答

0

出现这种情况的原因,你的排序方法的顺序数值为一个字符串,所以1500到来之前5

你需要转换之前比较数值500的原因1来过。

你需要的东西是这样的:(间HERE评论)

function sortTable(n) { 


var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; 
    table = document.getElementById("allCars"); 
    switching = true; 

    dir = "asc"; 

    while (switching) { 
     switching = false; 
     rows = table.getElementsByTagName("TR"); 
     for (i = 1; i < (rows.length - 1); i++) { 
     shouldSwitch = false; 
     x = rows[i].getElementsByTagName("TD")[n]; 
     y = rows[i + 1].getElementsByTagName("TD")[n]; 
     if (dir == "asc") { 
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { 
      shouldSwitch= true; 
      break; 
     } 
     } else if (dir == "desc") { 
     // START HERE 
     var value1 = x.innerHTML; 
     var value2 = y.innerHTML; 

     //CHECK IF VALUES ARE NUMERIC. (LINK LATER) 

     if (isNumeric(value1) && isNumeric(value2)){ 
      if(Number(value1) < Number(value2){ 
       shouldSwitch = true; 
       break; 
      } 
     } else { 

     // FINISH HERE 

      if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { 
       shouldSwitch = true; 
       break; 
     } 
     } 
    } 
    if (shouldSwitch) { 
     rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); 
     switching = true; 
     switchcount ++; 
    } else { 
     if (switchcount == 0 && dir == "asc") { 
     dir = "desc"; 
     switching = true; 
     } 
    } 
    } 
} 

如你本身,我的代码没有实现ISNUMERIC(值)功能,可以阅读validate decimal numbers in JavaScript来实现它!

+0

感谢您抽出时间来回答。这次我没有得到它的工作,截止日期过去了:)但下次我会记住这一点。 –