當我按任意列排序我的對象數組時,它工作正常...直到我的array.length> 10 ...在谷歌瀏覽器(仍然在Firefox中工作正常)。javascript array length> 10 sort google chrome
編輯:當數組的長度超過10的問題是,排序只是「隨機」排序我的數組。
所以我檢查了我的排序函數是否返回有效的(0,負數或正整數)和是的,他們這樣做。
我在返回之前做一個簡單的console.log右
console.log(a.wins - b.wins); // when comparing integers
console.log(a.name.localeCompare(b.name, undefined, {sensitivity: 'base'})); // when comparing strings
輸出,我在我的控制檯是唯一編號(-1,字符串comparaisons 0或1,選中此& -5〜 140爲整數比較)
任何想法解決我的問題?
EDIT2:好,讓我們嘗試把一些代碼,這裏是代碼時,我比較的用戶名,例如
// So, when I click on Username column of my table, it calls this function
// The array I need to sort & display is: summoners
// You can see that I'm doing something weird in case my username is empty,
// that's the way I found to put blank names after the 'z' letter so they
// appear at the end of the list when sorted ascending
if (jQuery(this).html() == 'Username'){
var clonedSummoners = _.map(summoners, _.clone);
summoners = summoners.sort(function(a, b){
var first, second;
if (a.Username == '')
first = 'zzzzzzzzzzzzzzzzzzzzzz';
else
first = a.Username;
if (b.Username == '')
second = 'zzzzzzzzzzzzzzzzzzzzzz';
else
second = b.Username;
console.log(first.localeCompare(second, undefined, {sensitivity: 'base'}));
return first.localeCompare(second, undefined, {sensitivity: 'base'});
});
var testSort = true;
for(var i=0; i<summoners.length; i++)
if (summoners[i].id != clonedSummoners[i].id)
testSort = false;
if (testSort == true)
summoners = summoners.reverse();
}
drawSummoners();
編輯3:所以,這裏是我想&我得到什麼:
比方說,我的數組是:
summoners = [
{'username' : 'calpton', 'age' : 19},
{'username' : 'comics', 'age' : 12},
{'username' : 'hello', 'age' : 17},
{'username' : 'newyork', 'age' : 47},
{'username' : 'physics', 'age' : 14},
{'username' : '', 'age' : 1},
{'username' : 'azerty', 'age' : 2},
{'username' : 'bob', 'age' : 5},
{'username' : 'hello', 'age' : 37},
{'username' : 'henry', 'age' : 17},
{'username' : 'therapy', 'age' : 27}
];
愈傷組織後,納克我上面的功能,我想我的召喚要排序的數組這樣
summoners = [
{'username' : 'azerty', 'age' : 2},
{'username' : 'bob', 'age' : 5},
{'username' : 'calpton', 'age' : 19},
{'username' : 'comics', 'age' : 12},
{'username' : 'hello', 'age' : 17}, // it doesn't matter
{'username' : 'hello', 'age' : 37}, // for these ones
{'username' : 'henry', 'age' : 17},
{'username' : 'newyork', 'age' : 47},
{'username' : 'physics', 'age' : 14},
{'username' : 'therapy', 'age' : 27},
{'username' : '', 'age' : 1},
];
,但我會得到相反:
summoners = [
{'username' : 'calpton', 'age' : 19},
{'username' : 'bob', 'age' : 5},
{'username' : 'hello', 'age' : 37},
{'username' : 'henry', 'age' : 17},
{'username' : 'comics', 'age' : 12},
{'username' : 'hello', 'age' : 17},
{'username' : 'newyork', 'age' : 47},
{'username' : 'physics', 'age' : 14},
{'username' : '', 'age' : 1},
{'username' : 'azerty', 'age' : 2},
{'username' : 'therapy', 'age' : 27}
];
所以,我想這是不是隨機的,因爲它總是會來排序這種方式,但這種排序毫無意義。
EDIT4:沒有,用戶名不返回未定義
get Username(){
if (typeof this.username == 'undefined')
return '';
else return this.username;
}
我在一類
最後編輯的背景: OK,我發現了問題是什麼,問題是我的數組是一個複雜的對象數組,我試圖通過一個屬性對它進行排序,但有時,我有這個屬性的值相同,就像11個對象中的7個一樣。我在Chrome中遇到的問題是,谷歌瀏覽器並不總是按相同的順序對相同的值進行排序,而Firefox則始終保持相同的順序。在我的代碼中(下面的Edit2),我檢查數組是否已經排序,如果是的話,我將它反轉,它在firefox中運行良好,因爲它總是相同的,而不是。我仍然沒有弄清楚它是如何搞砸最初排序的,但是我只是簡單地在我認爲是獨特的屬性上添加了第二種排序,並且它解決了所有問題。
return first.localeCompare(second, undefined, {sensitivity: 'base'}) || a.name.localeCompare(b.name, undefined, {sensitivity: 'base'});
該名稱總是存在且唯一,因此我的數組總是以相同的方式排序。
你能發佈一段代碼,直接演示你的問題? – Frxstrem
到目前爲止,您還沒有描述過這個問題。有什麼問題?它返回undefined?它返回一個空數組?它沒有排序?如果它沒有排序,你是否確定它沒有排序(根本沒有改變),還是以意想不到的順序排序? – slebetman
對不起@slebetman,我編輯了我的帖子來回答你的問題 – PineApple34