排序函数不能在升序和降序之间切换

sort function not toggling between ascending and descending

本文关键字:降序 之间 升序 函数 不能 排序      更新时间:2023-09-26

我目前正在处理一个表,它在纯js和jQuery中对cat数据进行排序。我快完成了。我不知道如何让我的表标题排序升序和降序。我尝试了各种方法,包括设置布尔变量。比如:

//pseudo code
            var ascending;
            if ascending = true
            var sortedData = cats.sort(function(a,b){return (a.country < b.country) ? -1 : 1;});
            else ...

目前,我的应用程序按字母顺序对每个列进行排序,但不会反向排序。下面是我的代码:

<!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta http-equiv="content-type" content="text/html;charset=Windows-1252">
                </head>
                <body>
                    <div id="catTable"></div>
                    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
                    <script>

                function renderData(cats){
                        var output='<table id="indextable" border="1" cellpadding="10" cellspacing="0" style="border-collapse:collapse;">';
                        output+="<thead>"
                        output+="<tr>";
                        output+="<th> HeadShot </th>";
                        output+="<th><button onclick='getSortedBreedData()'>Breed</button></th>";
                        output+="<th><button onclick='getSortedCountryData()'>Country</button></th>";
                        output+="<th><button onclick='getSortedCoffeeData()'>CoffeePreference</button></th>";
                        output+="</tr>";
                        output+="</thead>"
                    for (var i in cats) {
                        output+="<tbody>" 
                        output+="<tr>";
                        output+="<td><img src='" + cats[i].picture+"' alt='missing cat picture'></td>"
                        output+="<td>" + cats[i].breed + "</td>"
                        output+="<td>" + cats[i].country + "</td>"
                        output+="<td>" + cats[i].coffeePreference + "</td>"
                        output+="</tr>";
                        output+="</tbody>" 
                    }
                    output+="</table>";
                    document.getElementById("catTable").innerHTML=output;
                }            

                function getData(){       
                    $.getJSON('cats.json', function(cats) {
                        var cats = cats;
                        renderData(cats);    
                    });
                }
                function getSortedCountryData(){       
                    $.getJSON('cats.json', function(cats) {
                        var cats = cats;
                        sortData(cats,'country');    
                    });
                }    
                function getSortedBreedData(){       
                    $.getJSON('cats.json', function(cats) {
                        var cats = cats;
                        sortData(cats,'breed');    
                    });
                }    
                function getSortedCoffeeData(){       
                    $.getJSON('cats.json', function(cats) {
                        var cats = cats;
                        sortData(cats,'coffeePreference');    
                    });
                }
                function sortData(cats, element){
                    switch(element) {
                        case 'breed':
                            var sortedData = cats.sort(function(a,b){return (a.breed < b.breed) ? -1 : 1;});
                            renderData(cats);
                            break;
                        case 'country':
                            var sortedData = cats.sort(function(a,b){return (a.country < b.country) ? -1 : 1;});
                            renderData(cats);
                            break;
                        case 'coffeePreference':
                            var sortedData = cats.sort(function(a,b){return (a.coffeePreference < b.coffeePreference) ? -1 : 1;});
                            renderData(cats);
                        default:
                            renderData(cats);
                    }
                }


                getData();
                </script>
                </body>
                </html>

要对代码进行反向排序,

 cats.sort(function(a,b){return (a.country < b.country) ? 1 : -1;});

注意,1和-1是颠倒的。

您可以更新sortData函数以接收排序方向的附加参数。

function sortData(cats, element, direction)

和使用

cats.sort(function(a,b){return (a.country < b.country) ? -1*direction : 1*direction;});
使用

sortData(cats, element, 1) or sortData(cats, element, -1)