i found the following solution to add sorting capabilities to jQuery (ref: jQuery sort()). I've altered the solution to sort strings of unknown length. It works nicely, however as you might notice from the function name: it sorts acscending :-).
我发现以下解决方案为jQuery添加排序功能(ref:jQuery sort())。我已经改变了解决方案来排序未知长度的字符串。它可以很好地工作,但是正如您可能从函数名称中注意到的那样:它对accecending进行排序:-)。
jQuery.fn.sort = function() {
return this.pushStack( [].sort.apply( this, arguments ), []);
};
function sortStringAscending(a,b){
var h1 = a.innerHTML;
var h2 = b.innerHTML;
var c = 0;
var str1 = null,
var str2 = null;
while(str1 == str2 && (c < h1.length && c < h2.length)) {
str1 = h1.substring(c,c+1).toLowerCase().charCodeAt(0);
str2 = h2.substring(c,c+1).toLowerCase().charCodeAt(0);
if(str1 > str2) {
r = 1;
} else if(str2 > str1) {
r = -1;
}
c += 1;
}
return r;
};
The function is used like this:
该函数使用如下:
$('ol li').sort(sortStringAscending).appendTo('ol');
Is it possible to alter this code in such a way that the following will become possible?
是否有可能以下列方式更改此代码?
$('ol li').sort(sortString, 0).appendTo('ol'); //0 for descending
$('ol li').sort(sortString, 1).appendTo('ol'); //1 for ascending
5 个解决方案
#1
You are not going to be able to easily get an additional argument into the array sort function that the jQuery.fn.sort uses.
您无法轻松地在jQuery.fn.sort使用的数组排序函数中获得额外的参数。
It will be easier to use two separate functions, one for ascending, and one for descending, but keep the actual comparision in a third function.
使用两个单独的函数会更容易,一个用于升序,一个用于降序,但在第三个函数中保持实际比较。
function sortStringAscending(a,b) {
return sortString(a,b,1);
};
function sortStringDescending(a,b) {
return sortString(a,b,-1);
};
function sortString(a,b,direction) {
var h1 = a.innerHTML.toLowerCase();
var h2 = b.innerHTML.toLowerCase();
if(h1 > h2) {
r = 1*direction;
} else if(h2 > h1) {
r = -1*direction;
}
return r;
};
Also note that you can simply compare two strings, there is no need to compare them character by character, unless you want to have unpredictable sort order for items like "abc" and "abcd".
另请注意,您可以简单地比较两个字符串,不需要逐个字符地比较它们,除非您希望对“abc”和“abcd”等项目具有不可预测的排序顺序。
Then you should be able to do
那你应该可以做到
$('ol li').sort(sortStringAscending).appendTo('ol');
$('ol li').sort(sortStringDescending).appendTo('ol');
#2
This will do what you've asked: (use a parameter to decide sort order)
这将执行您的要求:(使用参数来确定排序顺序)
function sortString(a,b,direction){
var d = (direction?1:-1);
var h1 = a.html();
var h2 = b.html();
var c = 0;
var str1 = null,
var str2 = null;
while(str1 == str2 && (c < h1.length && c < h2.length)) {
str1 = h1.substring(c,c+1).toLowerCase().charCodeAt(0);
str2 = h2.substring(c,c+1).toLowerCase().charCodeAt(0);
if(str1 > str2) {
r = 1*d;
} else if(str2 > str1) {
r = -1*d;
}
c += 1;
}
return r;
};
#3
Changing this:
if(str1 > str2) {
r = 1;
} else if(str2 > str1) {
r = -1;
}
to this:
if(str1 > str2) {
r = -1;
} else if(str2 > str1) {
r = 1;
}
will swap the sort order.
将交换排序顺序。
EDIT:
Because of the way Javascript's array.sort(callback) function work it is not easy to just add a third parameter for the callback to accept, I will suggest using 2 separate functions mySortAsc and mySortDesc which in turn could be calling a third function and passing whatever parameters are needed.
由于Javascript的array.sort(回调)函数的工作方式不容易为回调添加第三个参数接受,我建议使用2个独立的函数mySortAsc和mySortDesc,这反过来可以调用第三个函数并传递无论需要什么参数。
#4
You might be able to use a closure. Although the javascript array sort method expects a 2 parameter function, you can bring other information into the callback function's scope by doing something like:
您可能可以使用闭包。虽然javascript数组排序方法需要2参数函数,但您可以通过执行以下操作将其他信息带入回调函数的范围:
function BiSort(direction)
{
return function( a, b )
{
var d = ( direction ? 1 : -1 ) ;
return ( a > b ? 1 : b > a ? -1 : 0 ) * d ;
}
}
The BiSort function returns a new function that has access to the outer function's parameter list. Now you can do this:
BiSort函数返回一个可以访问外部函数参数列表的新函数。现在你可以这样做:
var a = ["ant", "goat", "bat", "zebra", "yak"] ;
a.sort(BiSort(true)); // sorts ascending
a.sort(BiSort(false)); // sort descending
#5
I know this is old, but for future reference, I would make the 2 functions for asc/desc, then make a var point to one or the other based on what you need and then pass that var to the array.sort()
我知道这是旧的,但是为了将来的参考,我会为asc / desc创建2个函数,然后根据你需要的内容创建一个或者另一个var,然后将该var传递给array.sort()
function SortAsc(a, b){
}
function SortDesc(a, b){
}
var sortOrder
// if asc button clicked
sortOrder = SortAsc;
// if desc button clicked
sortOrder = SortDesc;
$('ol li').sort(sortOrder);
#1
You are not going to be able to easily get an additional argument into the array sort function that the jQuery.fn.sort uses.
您无法轻松地在jQuery.fn.sort使用的数组排序函数中获得额外的参数。
It will be easier to use two separate functions, one for ascending, and one for descending, but keep the actual comparision in a third function.
使用两个单独的函数会更容易,一个用于升序,一个用于降序,但在第三个函数中保持实际比较。
function sortStringAscending(a,b) {
return sortString(a,b,1);
};
function sortStringDescending(a,b) {
return sortString(a,b,-1);
};
function sortString(a,b,direction) {
var h1 = a.innerHTML.toLowerCase();
var h2 = b.innerHTML.toLowerCase();
if(h1 > h2) {
r = 1*direction;
} else if(h2 > h1) {
r = -1*direction;
}
return r;
};
Also note that you can simply compare two strings, there is no need to compare them character by character, unless you want to have unpredictable sort order for items like "abc" and "abcd".
另请注意,您可以简单地比较两个字符串,不需要逐个字符地比较它们,除非您希望对“abc”和“abcd”等项目具有不可预测的排序顺序。
Then you should be able to do
那你应该可以做到
$('ol li').sort(sortStringAscending).appendTo('ol');
$('ol li').sort(sortStringDescending).appendTo('ol');
#2
This will do what you've asked: (use a parameter to decide sort order)
这将执行您的要求:(使用参数来确定排序顺序)
function sortString(a,b,direction){
var d = (direction?1:-1);
var h1 = a.html();
var h2 = b.html();
var c = 0;
var str1 = null,
var str2 = null;
while(str1 == str2 && (c < h1.length && c < h2.length)) {
str1 = h1.substring(c,c+1).toLowerCase().charCodeAt(0);
str2 = h2.substring(c,c+1).toLowerCase().charCodeAt(0);
if(str1 > str2) {
r = 1*d;
} else if(str2 > str1) {
r = -1*d;
}
c += 1;
}
return r;
};
#3
Changing this:
if(str1 > str2) {
r = 1;
} else if(str2 > str1) {
r = -1;
}
to this:
if(str1 > str2) {
r = -1;
} else if(str2 > str1) {
r = 1;
}
will swap the sort order.
将交换排序顺序。
EDIT:
Because of the way Javascript's array.sort(callback) function work it is not easy to just add a third parameter for the callback to accept, I will suggest using 2 separate functions mySortAsc and mySortDesc which in turn could be calling a third function and passing whatever parameters are needed.
由于Javascript的array.sort(回调)函数的工作方式不容易为回调添加第三个参数接受,我建议使用2个独立的函数mySortAsc和mySortDesc,这反过来可以调用第三个函数并传递无论需要什么参数。
#4
You might be able to use a closure. Although the javascript array sort method expects a 2 parameter function, you can bring other information into the callback function's scope by doing something like:
您可能可以使用闭包。虽然javascript数组排序方法需要2参数函数,但您可以通过执行以下操作将其他信息带入回调函数的范围:
function BiSort(direction)
{
return function( a, b )
{
var d = ( direction ? 1 : -1 ) ;
return ( a > b ? 1 : b > a ? -1 : 0 ) * d ;
}
}
The BiSort function returns a new function that has access to the outer function's parameter list. Now you can do this:
BiSort函数返回一个可以访问外部函数参数列表的新函数。现在你可以这样做:
var a = ["ant", "goat", "bat", "zebra", "yak"] ;
a.sort(BiSort(true)); // sorts ascending
a.sort(BiSort(false)); // sort descending
#5
I know this is old, but for future reference, I would make the 2 functions for asc/desc, then make a var point to one or the other based on what you need and then pass that var to the array.sort()
我知道这是旧的,但是为了将来的参考,我会为asc / desc创建2个函数,然后根据你需要的内容创建一个或者另一个var,然后将该var传递给array.sort()
function SortAsc(a, b){
}
function SortDesc(a, b){
}
var sortOrder
// if asc button clicked
sortOrder = SortAsc;
// if desc button clicked
sortOrder = SortDesc;
$('ol li').sort(sortOrder);