javascript - 使用 jquery 对多列进行字母数字排序

标签 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"
}]

最佳答案

您可以用 '-' 拆分字符串,并将字母部分视为字符串,将数字视为数字。

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 使用 jquery 对多列进行字母数字排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41751446/

相关文章:

javascript - 申请:not against a group of element css selectors?的方法

javascript - 淡出、更改然后淡入文本并延迟

javascript - 从缩略图网格中选择多个图像

javascript - querySelector 搜索直系子代

javascript - 为什么我的 svelte {#each} block 不是响应式(Reactive)的?

javascript - 在 Javascript 中对具有多个值的数组进行排序

javascript - 包含在 CSS/JS/HTML 中时如何得出正确的目录路径?

javascript - 放大 Iframe 会影响 cordova iOS 应用程序中错误的光标位置

javascript - 在 ionic 4 中使用 .js 文件

javascript - 如何使用 jquery 或 javascript 从 html 表中删除 div 标签?