2014-02-12 52 views
0

我使用雙逗號(,,)作爲我的數組分割分隔符。所以,當我去拆我的數組,我使用:JavaScript Sort Array刪除特殊數組分割字符

var flt = localStorage.getItem('flt'); 
    var fltArray = new Array(); 
    var fltArray = flt.split(",,"); 

當我對數組進行排序,排序我在代碼的每個元素結束剔除逗號。所以,所有數組元素之間的分割變成「,」當它最初是「,」

我正在使用下面的代碼來排序我的數組。

fltOfferSellEconArray.sort(function(x,y){ 
     var xp = x.substr(0,4); 
     var yp = y.substr(0,4); 
     return xp == yp ? 0 : xp > yp ? -1 : 1; 
    }); 

有沒有辦法在每個元素的末尾添加一個逗號而不必再次遍歷數組?

這是在排序前我的數組.....

["0000999X12623220000009999999999,","0300199X1392215130873ySP3sBJLTe,","0301199X1392215191700ySP3sBJLTe,","0302199X1392215252490ySP3sBJLTe,","0303199X13922153062748VplSv6axJ,","0400199X13922155681178VplSv6axJ," ,"0375199X1392215732050ySP3sBJLTe,"] 

這是我需要它看起來像後排序。 (注...它只是移動開始「0400」到最後一個元素的元素.....

["0000999X12623220000009999999999,","0300199X1392215130873ySP3sBJLTe,","0301199X1392215191700ySP3sBJLTe,","0302199X1392215252490ySP3sBJLTe,","0303199X13922153062748VplSv6axJ,","0375199X1392215732050ySP3sBJLTe,","0400199X13922155681178VplSv6axJ,"] 
+3

你的問題不清楚你使用的序列化分隔符對排序無關 – Halcyon

+2

你能給出一個例子,說明你的原始數組看起來像什麼,它在排序後應該看起來像什麼樣的東西 –

+0

好的,我編輯了一個問題來顯示排序後我需要它的樣子,對不起。 – hypermiler

回答

1

這似乎爲我工作的罰款:

> a = ["0000999X12623220000009999999999,","0300199X1392215130873ySP3sBJLTe,","0301199X1392215191700ySP3sBJLTe,","0302199X1392215252490ySP3sBJLTe,","0303199X13922153062748VplSv6axJ,","0400199X13922155681178VplSv6axJ," ,"0375199X1392215732050ySP3sBJLTe,"] 
> a.sort(function(x,y){ 
    var xp = x.substr(0,4); 
    var yp = y.substr(0,4); 
    return xp == yp ? 0 : xp > yp ? -1 : 1; 
    }); 
> a 
["0400199X13922155681178VplSv6axJ,", "0375199X1392215732050ySP3sBJLTe,", "0303199X13922153062748VplSv6axJ,", "0302199X1392215252490ySP3sBJLTe,", "0301199X1392215191700ySP3sBJLTe,", "0300199X1392215130873ySP3sBJLTe,", "0000999X12623220000009999999999,"] 

究竟沒有按」 t爲你工作嗎?

+0

嗯。也許我會在其他地方出錯。研究....謝謝! – hypermiler

+0

謝謝大家。排序前我的陣列有缺陷。修正了這一切,一切工作正常。感謝@Claudiu指出這一點! – hypermiler