gpt4 book ai didi

javascript - 准备数组以在闭包中排序

转载 作者:行者123 更新时间:2023-11-29 22:20:38 27 4
gpt4 key购买 nike

根据我的研究和谷歌搜索,Javascript 似乎缺乏对区域设置感知排序和字符串比较的支持。有 localeCompare() ,但它一直是 reported of browser specific differencies 并且不可能明确设置使用哪个语言环境(操作系统语言环境并不总是想要的)。有一些 intentions to add collation support inside ECMAScript ,但在此之前,我们是靠自己的。并且取决于跨浏览器结果的一致性,我们可能永远只能靠自己了 :(。

我有以下代码,它按字母顺序对数组进行排序。考虑到速度,想法来自 https://stackoverflow.com/a/11598969/1691517 ,我对它进行了一些速度改进。

在这个例子中,单词数组有 13 个成员,排序函数被调用了 34 次。我想替换单词数组中的一些字母(你不必知道替换了什么,因为这不是本题的重点)。如果我在排序函数(以 return function(a, b) 开头的函数)中进行这些替换,则代码效率低下,因为每个数组成员进行了多次替换。当然,我可以在闭包之外进行这些替换,我的意思是在 words.sort(sortbyalphabet_timo); 行之前,但这不是我想要的。

问题 1:是否可以修改“PREPARATION STARTS”和“PREPARATION ENDS”行之间的单词数组,以便排序函数使用修改后的单词数组?

问题 2: 是否可以向闭包输入参数,以便 PREPARATION STARTS 和 PREPARATION ENDS 之间的代码可以使用它们?我试过这个但没有成功:

var caseinsensitive = true;
words.sort( sortbyalphabet_timo(caseinsensitive) );

最后是代码示例,准备运行的示例在 http://jsfiddle.net/3E7wb/ 中:

var sortbyalphabet_timo = (function() {
// PREPARATION STARTS
var i, alphabet = "-0123456789AaÀàÁáÂâÃãÄäBbCcÇçDdEeÈèÉéÊêËëFfGgHhIiÌìÍíÎîÏïJjKkLlMmNnÑñOoÒòÓóÔôÕõÖöPpQqRrSsTtUuÙùÚúÛûÜüVvWwXxYyÝýŸÿZz",
index = {};

i = alphabet.length;
while (i--) index[alphabet.charCodeAt(i)] = i;
// PREPARATION ENDS

return function(a, b) {
var i, len, diff;

if (typeof a === "string" && typeof b === "string") {
(a.length > b.length) ? len = a.length : len = b.length;
for (i = 0; i < len; i++) {
diff = index[a.charCodeAt(i)] - index[b.charCodeAt(i)];

if (diff !== 0) {
return diff;
}
}
// sort the shorter first
return a.length - b.length;
} else {
return 0;
}
};
})();

var words = ['tauschen', '66', '55', '33', 'täuschen', 'andern', 'ändern', 'Ast', 'Äste', 'dosen', 'dösen', 'Donaudam-0', 'Donaudam-1'];
$('#orig').html(words.toString());
words.sort(sortbyalphabet_timo);
$('#sorted').html(words.toString());`

最佳答案

Is it possible to modify the words-array in between the lines "PREPARATION STARTS" and "PREPARATION ENDS" so that the sort function uses modified words-array?

不,不是真的。您无权访问数组本身,您的函数仅构建稍后在数组上调用 .sort 时使用的比较函数。如果您需要更改数组,则需要编写一个将其作为参数获取的函数;例如,您可以在 Array.prototype 上添加一个方法。看起来像

function mysort(arr) {
// Preparation
// declaration of compare function
// OR execution of closure to get the compare function
arr.sort(comparefn);
return arr;
}

Is it possible to input arguments to the closure so that code between PREPARATION STARTS and PREPARATION ENDS can use them?

是的,当然 - 这就是使用闭包的原因 :-) 但是,您不能在当前代码中使用 sortbyalphabet_timo(caseinsensitive)。您拥有的闭包会立即被调用(称为 IIFE)并返回比较函数,您在演示中将其传递给 sort。

如果你想让 sortbyalphabet_timo 成为闭包而不是结果,你必须删除它后面的括号。您还可以在那里使用参数,这些参数在整个闭包范围内都可以访问(包括比较函数):

var sortbyalphabet_timo_closure = function(caseinsensitive) {
// Preparation, potentially using the arguments
// Declaration of compare function, potentially using the arguments
return comparefn;
}
// then use
words.sort(sortbyalphabet_timo_closure(true));

目前,您正在这样做:

var sortbyalphabet_timo_closure = function(/*having no arguments*/) {
// Preparation, potentially using the arguments
// Declaration of compare function, potentially using the arguments
return comparefn;
}
var sortbyalphabet_timo = sortbyalphabet_timo_closure();
// then use
words.sort(sortbyalphabet_timo);

...如果您需要多次排序,它只是缓存执行闭包的结果。

关于javascript - 准备数组以在闭包中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12606722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com