使用jquery进行多列数字排序

时间:2017-01-19 20:50:15

标签: javascript jquery html

我有一个混合数组,我需要按字母顺序排序,然后按数字

排序
[Ab-1,Ab-5,Ab-11,ab-101,ab-100,ab-10,ab-12,ab-3,ab-21]

根据代码,我得到以下输出

ab-1 ab-5 ab-11 ab-101 ab-100 ab-10 ab-12 ab-3 ab-21

根据代码,默认情况下,第三列单独显示活动和非活动。但我想要第一列(ID)也将它排序为:

ab-1
ab-2
ab-3
ab-5
ab-10
ab-11
ab-12
ab-100
ab-101....

请帮助

    <!DOCTYPE HTML>
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    // Fetch the values from json
    $(document).ready(function() {  
                        $.getJSON("dynamic_content_values1.json",   

/* sort starts for Active and Inactive */                                       
                                        function(result_1) {

                                        function sortJSON(data, key) {
                                        return data.sort(function (a, b) {
                                        var x = a[key];
                                        var y = b[key];
                                        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
                                        });
                                        }

                                        sorting_active_and_inactive = sortJSON(result_1, 'flag'); 
/* sort End for Active and Inactive*/
                                            $.each(sorting_active_and_inactive,function(i, field) {
                                                var status = field.flag;

                                                if (status == 'N') {
                                                    status = 'Active';
                                                } else {
                                                    status = 'Inactive';
                                                }
                                                var data = '<tr><td><a  href="#">' + field.first_column + '</a></td><td>' + field.summary + '</td><td>' + status + '</td></tr>';
                                                $("#resultBody").append(data);
                                            });
                                        });



    //******************************************* Starting CODE for Sorting the Active/In-Active Columns *******************************************    


                            function sortTable(table, col, reverse) {
                                var tb = table.tBodies[0], // use `<tbody>` to ignore `<thead>` and `<tfoot>` rows
                                    tr = Array.prototype.slice.call(tb.rows, 0), // put rows into array
                                    i;
                                reverse = -((+reverse) || -1);
                                tr = tr.sort(function (a, b) { // sort rows
                                    return reverse // `-1 *` if want opposite order
                                        * (a.cells[col].textContent.trim() // using `.textContent.trim()` for test
                                            .localeCompare(b.cells[col].textContent.trim())
                                           );
                                });
                                for(i = 0; i < tr.length; ++i) tb.appendChild(tr[i]); // append each row in order
                            }

                            function makeSortable(table) {
                                var th = table.tHead, i;
                                th && (th = th.rows[0]) && (th = th.cells);
                                if (th) i = th.length;
                                else return; // if no `<thead>` then do nothing
                                while (--i >= 0) (function (i) {
                                    var dir = 1;
                                    th[i].addEventListener('click', function () {sortTable(table, i, (dir = 1 - dir))});
                                }(i));
                            }

                            function makeAllSortable(parent) {
                                parent = parent || document.body;
                                var t = parent.getElementsByTagName('table'), i = t.length;
                                while (--i >= 0) makeSortable(t[i]);
                            }

                            window.onload = function () {makeAllSortable();};

//************************************************** Ending CODE for Sorting the Active/In-Active Columns *********************************
                    });



    function showabc() {
        document.getElementById('container1').style.display = "block";
    }
</script>
</head>
<body>
    <div class="" id="container1">
        <table>
            <thead>
                <tr>
                    <th><b>ID</b></th>
                    <th><b>Summary</b></th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody id="resultBody" style="cursor: move">
            </tbody>
        </table>
    </div>
    <div class="containerPopup1"><div id="abc1" style="height: 1400px;"></div></div>
    <div  class="containerPopup"><div id="abc2" style="height: 2700px;"></div></div>
</body>
</html>

请找到示例json数组结构:

    [ {
    "first_column": "AB-1",
    "summary": "AB-1 Summary",
    "flag": "N"
}, {
    "first_column": "AB-11",
    "summary": "AB-11 Summary",
    "flag": "N"
},{
    "first_column": "AB-12",
    "summary": "AB-12 Summary",
    "flag": "Y"
},{
    "first_column": "AB-10",
    "summary": "AB-10 Summary",
    "flag": "Y"
},{
    "first_column": "AB-100",
    "summary": "AB-100 Summary",
    "flag": "Y"
},{
    "first_column": "AB-101",
    "summary": "AB-101 Summary",
    "flag": "N"
},{
    "first_column": "AB-2",
    "summary": "AB-2 Summary",
    "flag": "Y"
},{
    "first_column": "AB-3",
    "summary": "AB-3 Summary",
    "flag": "Y"
},{
    "first_column": "AB-21",
    "summary": "AB-21 Summary",
    "flag": "Y"
},{
    "first_column": "AB-210",
    "summary": "AB-210 Summary",
    "flag": "Y"
},{
    "first_column": "AB-211",
    "summary": "AB-211 Summary",
    "flag": "Y"
},{
    "first_column": "AB-5",
    "summary": "AB-5 Summary",
    "flag": "N"
}]

2 个答案:

答案 0 :(得分:0)

您可以将字符串拆分为mysql_query(" INSERT into `new_table` (`hash`,`first_visit`, `country`, `browser`) values ('$hash',CURDATE(), '$details->geoplugin_countryCode','$_SERVER[HTTP_USER_AGENT]'); INSERT into `behaviour` (`hash`,`page`,`url`,`entry`, `ref`) values ('$hash','$page','$url','$entry', '$ref') "); ,并将字母部分视为字符串,将数字视为数字。

'-'
var array = [{ notificationID: "AB-1", summary: "AB-1 Summary", flag: "N" }, { notificationID: "AB-11", summary: "AB-11 Summary", flag: "N" }, { notificationID: "AB-12", summary: "AB-12 Summary", flag: "Y" }, { notificationID: "AB-10", summary: "AB-10 Summary", flag: "Y" }, { notificationID: "AB-100", summary: "AB-100 Summary", flag: "Y" }, { notificationID: "AB-101", summary: "AB-101 Summary", flag: "N" }, { notificationID: "AB-2", summary: "AB-2 Summary", flag: "Y" }, { notificationID: "AB-3", summary: "AB-3 Summary", flag: "Y" }, { notificationID: "AB-21", summary: "AB-21 Summary", flag: "Y" }, { notificationID: "AB-210", summary: "AB-210 Summary", flag: "Y" }, { notificationID: "AB-211", summary: "AB-211 Summary", flag: "Y" }, { notificationID: "AB-5", summary: "AB-5 Summary", flag: "N" }];

array.sort(function (a, b) {
     function split(s) { return s.split('-'); }
  
     var aa = split(a.notificationID),
         bb = split(b.notificationID);
  
     return aa[0].localeCompare(bb[0]) || aa[1] - bb[1];
});

console.log(array);

答案 1 :(得分:-1)

var a=[ {
    "LIST": "14:NATURAL RESOURCES"
},
{
    "LIST": "7:DIVERSITY IN LIVING ORGANISMS"
},
{
    "LIST": "3:ATOMS AND MOLECULES"
},
{
    "LIST": "10:GRAVITATION"
},
{
    "LIST": "6:TISSUES"
}]

var c=a.sort(function(a,b){
let aa=a.LIST,bb=b.LIST;
return parseInt(aa.split(":")[0]-bb.split(":")[0])

})

console.log(c)