Javascript 排序函数不在升序和降序之间切换
我目前正在处理一个表,它在纯js和jQuery中对cat数据进行排序。我快做完了。我不知道如何让我的表标题按升序和降序排序。我尝试过各种方法,包括设置布尔变量。有些类似:Javascript 排序函数不在升序和降序之间切换,javascript,jquery,sorting,Javascript,Jquery,Sorting,我目前正在处理一个表,它在纯js和jQuery中对cat数据进行排序。我快做完了。我不知道如何让我的表标题按升序和降序排序。我尝试过各种方法,包括设置布尔变量。有些类似: //pseudo code var ascending; if ascending = true var sortedData = cats.sort(function(a,b){return (a.country < b.country) ? -1
//pseudo code
var ascending;
if ascending = true
var sortedData = cats.sort(function(a,b){return (a.country < b.country) ? -1 : 1;});
else ...
//伪代码
var上升;
如果升序=真
var sortedData=cats.sort(函数(a,b){return(a.country
目前,我的应用程序按字母顺序对每一列进行排序,但不会反向排序。这是我的密码:
<!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>
函数渲染数据(CAT){
var输出=“”;
输出+=“”
输出+=“”;
输出+=“头像”;
输出+=“品种”;
产出+=“国家”;
输出+=“咖啡偏好”;
输出+=“”;
输出+=“”
for(猫的var i){
输出+=“”
输出+=“”;
输出+=“”
输出+=“”+猫[i]。繁殖+“”
输出+=“”+猫[i]。国家+“”
输出+=“”+cats[i]。咖啡偏好+“”
输出+=“”;
输出+=“”
}
输出+=“”;
document.getElementById(“catTable”).innerHTML=输出;
}
函数getData(){
$.getJSON('cats.json',函数(cats){
猫=猫;
renderData(猫);
});
}
函数getSortedCountryData(){
$.getJSON('cats.json',函数(cats){
猫=猫;
sortData(猫,'国家');
});
}
函数getSortedBreedData(){
$.getJSON('cats.json',函数(cats){
猫=猫;
sortData(猫,'品种');
});
}
函数getSortedCoffeeData(){
$.getJSON('cats.json',函数(cats){
猫=猫;
sortData(猫,“咖啡偏好”);
});
}
函数排序数据(CAT、元素){
开关(元件){
“品种”案例:
var sortedData=cats.sort(函数(a,b){return(a.bride
如果您有任何帮助或建议,我们将不胜感激。要对代码进行反向排序,请
cats.sort(function(a,b){return (a.country < b.country) ? 1 : -1;});
和使用
cats.sort(function(a,b){return (a.country < b.country) ? -1*direction : 1*direction;});
要进行反向排序,则代码应为
cats.sort(function(a,b){return (a.country < b.country) ? 1 : -1;});
和使用
cats.sort(function(a,b){return (a.country < b.country) ? -1*direction : 1*direction;});
感谢您的支持或支持谢谢@Rejoy。上面的更改带有一点逻辑性,我的表头是排序和反向排序。谢谢@Rejoy。上面的更改带有一点逻辑,我的表头是排序和反向排序。