排序数组只从表中拉出一个整数

时间:2016-09-10 05:03:31

标签: javascript arrays string sorting innerhtml

我相信解决方案是在innerhtml中,但尚未提出解决方案。我对编码很新,并且到目前为止已经有了一些外部帮助,但正如你可以看到数组有问题,因为它只是拉动表体中每个数字的第一个整数。我确信这很容易解决。

<html>

<head>
    <meta http-equiv="content-type" content="text/html;charset=Windows-1252">
    <script type="text/javascript">
        var people, asc1 = 1,
            asc2 = 1,
            asc3 = 1,
        asc4 = 1,
        asc5 = 1,
        asc6 = 1,
        asc7 = 1,
        asc8 = 1,
        asc9 = 1,
        asc10 = 1,
        asc11 = 1
        asc12 = 1;
        window.onload = function () {
            people = document.getElementById("people");
        }
    function sort_table(tbody, col, asc) {
            var rows = tbody.rows,
                rlen = rows.length,
                arr = new Array(),
                i, j, cells, clen;
            // fill the array with values from the table
            for (i = 0; i < rlen; i++) {
                cells = rows[i].cells;
                clen = cells.length;
                arr[i] = new Array();
                for (j = 0; j < clen; j++) {
                    arr[i][j] = cells[j].innerHTML;
                }
            }
            // sort the array by the specified column number (col) and order     (asc)
            arr.sort(function (a, b) {
                return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1 * asc);
            });
            // replace existing rows with new rows created from the sorted array
            for (i = 0; i < rlen; i++) {
                rows[i].innerHTML = "<td>" + arr[i].join("</td><td>") + "</td>";
            }
        }
    </script>
    <style type="text/css">
        table {
            border-collapse: collapse;
            border: none;
        }
        th,
        td {
            border: 1px solid black;
            padding: 4px 16px;
            font-family: Times New Roman;
            font-size: 24px;
            text-align: left;
        }
        th {
            background-color: #C8C8C8;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <tr>
                <th onclick="sort_table(people, 0, asc1); asc1 *= -1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1;">Player</th>
                <th onclick="sort_table(people, 1, asc2); asc2 *= -1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1;">Team</th>
                <th onclick="sort_table(people, 2, asc3); asc3 *= -1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1;">Role</th>
        <th onclick="sort_table(people, 3, asc4); asc4 *= -1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1;">KDA</th>
        <th onclick="sort_table(people, 4, asc5); asc5 *= -1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1;">Kills</th>
        <th onclick="sort_table(people, 5, asc6); asc6 *= -1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1;">Deaths</th>
        <th onclick="sort_table(people, 6, asc7); asc7 *= -1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1;">Assists</th>
        <th onclick="sort_table(people, 7, asc8); asc8 *= -1; asc9 = 1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1;">KP(%)</th>
        <th onclick="sort_table(people, 8, asc9); asc9 *= -1; asc10 = 1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1;">CS(Total)</th>  
        <th onclick="sort_table(people, 9, asc10); asc10 *= -1; asc11 = 1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1;">CS(p/m)</th>
        <th onclick="sort_table(people, 10, asc11); asc11 *= -1; asc12 = 1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1;">Gold(~Thousands)</th>
        <th onclick="sort_table(people, 11, asc12); asc12 *= -1; asc1 = 1; asc2 = 1; asc3 = 1; asc4 = 1; asc5 = 1; asc6 = 1; asc7 = 1; asc8 = 1; asc9 = 1; asc10 = 1; asc11 =1;">Games Played</th>
            </tr>
        </thead>
        <tbody id="people">
            <tr>

        <tr><td>Anivia HD</td><td>Apox</td><td>Mid</td><td>1.1</td><td>9</td>  <td>16</td><td>8</td><td>64.5</td><td>664</td><td>6.2</td><td>33</td><td>4</td></tr>   
        <tr><td>Zeabard</td><td>Apox</td><td>ADC</td><td>1.6</td><td>5</td><td>12</td><td>10</td><td>61.75</td><td>611</td><td>5.8</td><td>32</td><td>4</td></tr>
        <tr><td>MurkHexis</td><td>Apox</td><td>Support</td><td>2.8</td><td>1</td><td>14</td><td>13</td><td>71.25</td><td>60</td><td>0.6</td><td>23</td><td>4</td></tr>
        <tr><td>JMLCN</td><td>Apox</td><td>Jungle</td><td>0.8</td><td>6</td><td>20</td><td>11</td><td>78.25</td><td>255</td><td>2.5</td><td>28</td><td>4</td></tr>
        <tr><td>tlaliquid sen</td><td>Apox</td><td>Top</td><td>0.8</td><td>5</td><td>22</td><td>12</td><td>62.25</td><td>627</td><td>5.8</td><td>31</td><td>4</td></tr>
        <tr><td>Higginbottomz</td><td>NVA</td><td>Mid</td><td>11.3</td><td>7</td><td>3</td><td>20</td><td>48.5</td><td>400</td><td>5.9</td><td>24</td><td>2</td></tr>
        <tr><td>iFeederDog</td><td>NVA</td><td>Jungle</td><td>17.5</td><td>9</td><td>0</td><td>26</td><td>63.5</td><td>288</td><td>4.3</td><td>26</td><td>2</td></tr>
        <tr><td>s Herny</td><td>NVA</td><td>Top</td><td>13.8</td><td>16</td><td>3</td><td>20</td><td>65.5</td><td>423</td><td>6.5</td><td>28</td><td>2</td></tr>

 </tr>

1 个答案:

答案 0 :(得分:0)

你在这里缺少一个逗号:asc10 = 1,asc11 = 1 asc12 = 1;

很多事情可以做得更好,但你的主要问题是你正在排序字符串,这就是数字字段排序不正确的原因。在使用arr.sort()之前,必须将字符串转换为整数。

在使用arr.sort()对它们进行排序之前,请对使用整数填充的列使用parseInt()。因为您将它们视为数字但实际上它们是排序方法的字符串,因此您应将它们转换为数字。