2010-06-02 47 views
4

我有一個javascript函數中的一行,它根據另一個字符串數組的順序對一個對象數組進行排序。這是在Firefox的工作,但不是在IE瀏覽器,我不知道爲什麼。以下是我的數據在IE7中進入排序調用的樣子。 (爲了說明這一點,我使用了三個項目的數組)。JS排序在Firefox中工作,但不是IE - 不能解決爲什麼

//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"} 
] 

下面是實際的排序代碼:

csubSorted = csub.sort(function(a,b){ return (originalData.indexOf(a.value) > originalData.indexOf(b.value)); }); 

任何人都可以明白爲什麼這是行不通的?基本的JavaScript排序功能不是跨瀏覽器兼容的?我可以用不同的方式來做到這一點(例如與jQuery),這將是跨瀏覽器?

感謝您的任何意見 - 最大

編輯 - 這也失敗在Safari和Chrome的工作 - 換句話說,它似乎只在Firefox工作。

已解決 - 感謝Tim Down。 我實際上讓我的代碼更簡單,因爲我意識到我需要的順序總是「返回數組中的第一項,然後是使用.value排序的數組的其餘部分」。所以,我因此改變了我的代碼:

first = csub.shift(); 
    csubSorted = csub.sort(function(a,b){ 
    return (a.value > b.value); 
    }); 
    csubSorted.unshift(first); 

但是,它仍然不能正常工作。然後Tim(下面)指出排序期望從函數返回-1,0或1,不是真或假,這是我的代碼返回的。很顯然,firefox可以讓你擺脫這種困境,但其他瀏覽器卻沒有。所需要的只是將真或假轉化爲1和-1(我不擔心兩個字符串都是字符串的情況,實際上這會返回-1,這不會對反正排序):

first = csub.shift(); 
    csubSorted = csub.sort(function(a,b){ 
    return (a.value > b.value ? 1 : -1); 
    }); 
    csubSorted.unshift(first); 

添還告訴我,array.indexOf()沒有在IE瀏覽器的,即使我不在這裏使用它是煩人支持更多的我在其他位使用它的代碼。 Goddamit。有沒有一個API頁面明確列出了跨瀏覽器兼容的javscript API?

+0

您可以在IE上通過取消選中disable debugging.search來調試您的代碼,以在IE中進行調試。 – Salil 2010-06-02 09:39:30

回答

9

首先,在IE < = 8中沒有indexOf數組的方法。您需要編寫自己的數組。其次,傳遞給數組的sort()方法的比較函數應返回一個數字而不是布爾值。

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); 
}); 
+0

這篇文章解釋瞭如何添加它:http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-ie-browsers – Koen 2010-06-02 09:44:39

+0

我提供了我自己的簡單版本的indexOf '不完全滿足規範,但在這裏完成任務,並且不會增加'Array.prototype'。 「 – 2010-06-02 09:49:19

+5

ahh - 就是這樣」第二,傳遞給數組的sort()方法的比較函數應該返回一個數字而不是布爾值。「那讓我很興奮。我實際上讓我的代碼更簡單,所以我不需要再做indexOf,但它仍然不起作用。詳情請參閱原始描述。 – 2010-06-02 10:17:27

相關問題