问题描述
我在javascript函数中有一行,它根据另一个字符串数组的顺序对一个对象数组进行排序。这是在Firefox中工作但不在IE中,我不知道为什么。这是我的数据在IE7中进入排序调用的样子。 (我正在使用三个项目的数组来说明这一点)。
I have a line in a javascript function that sort an array of objects based on the order of another array of strings. This is working in firefox but not in IE and i don't know why. Here's what my data looks like going into the sort call, in IE7. (I'm using an array of three items just to illustrate the point here).
//cherry first then the rest in alphabetical order
originalData = ['cherry','apple','banana','clementine','nectarine','plum']
//data before sorting - note how clementine is second item - we wan to to to be after apple and banana
csub = [
{"value":"cherry","data":["cherry"],"result":"cherry"},
{"value":"clementine","data":["clementine"],"result":"clementine"},
{"value":"apple","data":["apple"],"result":"apple"},
{"value":"banana","data":["banana"],"result":"banana"},
{"value":"nectarine","data":["nectarine"],"result":"nectarine"},
{"value":"plum","data":["plum"],"result":"plum"}
]
//after sorting, csub has been rearranged but still isn't right: clementine is before banana. in FF it's in the right place.
csubSorted = [
{"value":"cherry","data":["cherry"],"result":"cherry"},
{"value":"apple","data":["apple"],"result":"apple"},
{"value":"clementine","data":["clementine"],"result":"clementine"},
{"value":"banana","data":["banana"],"result":"banana"},
{"value":"nectarine","data":["nectarine"],"result":"nectarine"},
{"value":"plum","data":["plum"],"result":"plum"}
]
以下是实际的排序代码:
Here's the actual sort code:
csubSorted = csub.sort(function(a,b){ return (originalData.indexOf(a.value) > originalData.indexOf(b.value)); });
有谁能看出为什么这不起作用?基本的javascript排序功能是不是跨浏览器兼容的?我可以用不同的方式(例如使用jquery)进行跨浏览吗?
Can anyone see why this wouldn't work? Is the basic javascript sort function not cross-browser compatible? Can i do this a different way (eg with jquery) that would be cross-browser?
感谢任何建议 - 最多
grateful for any advice - max
编辑 - 这也无法在safari和chrome中工作 - 换句话说,它似乎只能在firefox中运行。
EDIT - this also fails to work in safari and chrome - in other words, it only seems to work in firefox.
已解决 - 感谢Tim Down 。
我实际上使我的代码更简单,因为我意识到我需要的顺序始终是返回数组中的第一项,然后是使用.value排序的其余数组。所以,我改变了我的代码:
SOLVED - thanks to Tim Down.I'd actually made my code simpler because i realised that the order that i needed was always "the first item in the returned array followed by the rest of the array sorted using .value". So, i changed my code thus:
first = csub.shift();
csubSorted = csub.sort(function(a,b){
return (a.value > b.value);
});
csubSorted.unshift(first);
但是,它仍然无效。然后Tim(下面)指出sort希望从函数返回-1,0或1,not true或false,这是我的代码返回的。显然,firefox可以让你逃避这一点,但其他浏览器却没有。所需要的只是将'或'转换为真或假为1和-1(我不担心两个字符串都是samae的情况,实际上会返回为-1,这对于无论如何排序顺序):
But, it still wasn't working. Then Tim (below) pointed out that sort expects to get a -1, 0 or 1 back from the function, NOT true or false which is what my code was returning. Obviously firefox lets you get away with this, but the other browsers don't. All that was required was to 'translate' true or false into 1 and -1 (i don't worry about the case where both strings are the samae, effectively that will get returned as -1 which wouldn't make any difference to the sort order anyway):
first = csub.shift();
csubSorted = csub.sort(function(a,b){
return (a.value > b.value ? 1 : -1);
});
csubSorted.unshift(first);
Tim还告诉我,IE中不支持array.indexOf(),这甚至令人讨厌虽然我不在这里使用它,我在其他代码中使用它。 Goddamit。是否有某个API页面最终只列出了跨浏览器兼容的javscript API?
Tim also told me that array.indexOf() isn't supported in IE which is annoying as even though i'm not using it here any more i am using it in other bits of code. Goddamit. Is there an API page somewhere which definitively lists only the cross-browser compatible javscript API?
推荐答案
首先,没有 indexOf
IE中的数组方法< = 8.您需要自己编写。其次,传递给Array的 sort()
方法的比较函数应该返回一个数字而不是一个布尔值。
First, there's no indexOf
method of Array in IE <= 8. You'll need to write your own. Second, the comparison function passed to the sort()
method of an Array should return a number rather than a Boolean.
var indexOf = (typeof Array.prototype.indexOf == "function") ?
function(arr, val) {
return arr.indexOf(val);
} :
function(arr, val) {
for (var i = 0, len = arr.length; i < len; ++i) {
if (typeof arr[i] != "undefined" && arr[i] === val) {
return i;
}
}
return -1;
};
csubSorted = csub.sort(function(a,b){
return indexOf(originalData, a.value) - indexOf(originalData, b.value);
});
这篇关于JS排序适用于Firefox但不适用于IE - 无法解决原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!