在 Javascript 中切换数组排序(升序到降序,反之亦然)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14493985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Toggle array sort in Javascript (ascending to descending and vice versa)
提问by Samir Karki
I am sorting my array like this:
我正在像这样对我的数组进行排序:
array.sort((function(index) {
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 :1));
};
})(0));
As you can see, it is sorted in ascending order.
如您所见,它按升序排序。
My question is how do I toggle sorting? For example, if it is already in ascending order then how can I sort it in descending order and vice-versa?
我的问题是如何切换排序?例如,如果它已经按升序排列,那么我如何按降序对其进行排序,反之亦然?
I know to sort in descending I need to modify code like this:
我知道降序排序我需要修改这样的代码:
array.sort((function(index) {
return function(a, b) {
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? 1 :-1));
};
})(0));
but I don't know how to toggle.
但我不知道如何切换。
采纳答案by VinayC
If you know for certain that array is sorted then you can reverse the order by using a simple loop
如果您确定该数组已排序,则可以使用简单的循环来反转顺序
var l = array.length;
for(i=0; i< l / 2; i++) {
var t = array[i];
array[i] = array[l - 1 - i];
array[l - 1 - i] = t;
}
More simpler solution is to use reversefunction (BTW, check this SO Q&Afor different reversing algo and their performance)
更简单的解决方案是使用reverse函数(顺便说一句,请查看此 SO Q&A以了解不同的反向算法及其性能)
If you don't know the initial state of you array then I will advise associating a custom property to an array that will track the sort order. For example,
如果您不知道数组的初始状态,那么我建议将自定义属性关联到将跟踪排序顺序的数组。例如,
function sortArray(a, isAscending) {
var currentSort = a["my_sort_order"];
if (typeof currentSort != 'boolean') {
// assume it be unsorted, use sort alogorithm
a.sort(function(a,b) { return isAscending ? a - b : b - a; }); // assuming numerical array, modify as per your needs
} else if (currentSort != isAscending) {
// sorted but in different order, reverse the order
a.reverse(); // or use for loop
}
// set the sort order
a["my_sort_order"] = isAscending ? true : false;
}
回答by Samir Karki
.reverse() will always reverse the order of an array, so on toggling, you just can call yourSortedArray.reverse()
.reverse() 将始终反转数组的顺序,因此切换时,您只需调用 yourSortedArray.reverse()
var myArray = [1, 5, 8, 4, 0, 3, 6];
myArray.sort(); //[0, 1, 3, 4, 5, 6, 8]
myArray.reverse(); //[8, 6, 5, 4, 3, 1, 0]
回答by pete
How about:
怎么样:
var array = [ 2,4,7,12,1,5 ];
array.toggled_sort = function () {
var self=this;
this.asc=!this.asc;
return this.sort(function (l, r) {
return l > r ? (self.asc ? 1 : -1) : l < r ? (self.asc ? -1 : 1) : 0;
});
};
array.toggled_sort(); // ==> [ 1,2,4,5,7,12 ]
array.toggled_sort(); // ==> [ 12,7,5,4,2,1 ]
array.toggled_sort(); // ==> [ 1,2,4,5,7,12 ]
array.toggled_sort(); // ==> [ 12,7,5,4,2,1 ]
// etc.
回答by Shanimal
You were on the right track, you needed a third closure to store the state of toggle.
你在正确的轨道上,你需要第三个闭包来存储切换的状态。
function fn(reversed){
return function(){
reversed = !reversed;
return function(a,b){
return (a==b ? 0 : a < b? -1 : 1) * (reversed ? -1 : 1);
};
};
};
// usage
var toggleSort = fn();
array.sort(toggleSort())
jsfiddle: http://jsfiddle.net/8JMuj/1/
jsfiddle:http: //jsfiddle.net/8JMuj/1/

