2013-09-26 110 views
4
var myArray = [ 
    '_aaaa_2013-09-25_ssss9.txt', 
    '_aaaa_2013-09-25_ssss8.txt', 
    '_aaaa_2013-09-26_ssss1.txt', 
    '_aaaa_2013-09-25_ssss10.txt', 
    '_aaaa_2013-09-26_ssss2.txt', 
    '_aaaa_2013-09-25_ssss13.txt', 
    '_aaaa_2013-09-25_ssss5.txt', 
    '_aaaa_2013-09-25_ssss6.txt', 
    '_aaaa_2013-09-25_ssss7.txt' 
]; 

我需要按日期和數字對數組進行排序。排序包含字符串,日期和數字的數組

結果應該是

var result = [ 
    '_aaaa_2013-09-25_ssss5.txt', 
    '_aaaa_2013-09-25_ssss6.txt', 
    '_aaaa_2013-09-25_ssss7.txt', 
    '_aaaa_2013-09-25_ssss8.txt', 
    '_aaaa_2013-09-25_ssss9.txt', 
    '_aaaa_2013-09-25_ssss13.txt', 
    '_aaaa_2013-09-26_ssss1.txt', 
    '_aaaa_2013-09-26_ssss2.txt' 
]; 

我曾嘗試以下代碼。此將僅日期,進行排序,但我需要的是之前」我.txt'.How可以做到這一點的數量進行排序。

myArray.sort(function (a, b) { 

    var timeStamp1 = a.substring(a.indexOf('_aaaa') + 6, a.indexOf('_ssss')); 
    var timeStamp2 = b.substring(b.indexOf('_aaaa') + 6, b.indexOf('_ssss')); 
    timeStamp1 = new Date(Date.UTC(timeStamp1[0], timeStamp1[1], timeStamp1[2])); 
    timeStamp2 = new Date(Date.UTC(timeStamp2[0], timeStamp2[1], timeStamp2[2])); 

    return (timeStamp1 > timeStamp2) ? 1 : (timeStamp2 > timeStamp1 ? -1 : 0); 

}); 
+0

@sachin:您可以通過數字比較任意年月日的日期。您甚至可以將您的YYYY-MM-DD日期作爲字符串進行比較,但這不是必需的。和比較數字是微不足道的(搜索它,如果你不知道它) – Bergi

+0

我只是嘗試在Ela公佈的鏈接和Bergi爲您的用例建議的接受解決方案,它按您的需要排序。 –

+0

@Bergi我已經嘗試過,但那是行不通的。 – sachin

回答

1

你可以做這樣的:

var re = /^_aaaa_(\d\d\d\d-\d\d-\d\d)_ssss(\d+)\.txt$/; 

var result = myArray.slice().sort(function(a, b) { 
    var aa = a.match(re), bb = b.match(re); 
    return(
     aa[1] < bb[1] ? -1 : 
     aa[1] > bb[1] ? 1 : 
     aa[2] - bb[2] 
    ); 
}); 

注意使用.slice()創建數組的一個副本。如果您想對原始數組進行排序,則可以省略此項。 (感謝@DerFlatulator提醒!)

+0

嗨,我試過這個,但即時通訊錯誤在這一行:aa [1] sachin

+1

那個錯誤意味着正則表達式與數組元素中的文本不匹配。因此,您的數據看起來似乎與您在問題中發佈的內容有所不同。如果您將'myArray'的定義粘貼到Chrome控制檯中,然後粘貼上面的代碼,'result'的值將會是您要查找的內容。你的數據或代碼有什麼不同? –

+0

@DerFlatulator我的數據是:cccc_aaaa_2013-09-26_ssss1.txt – sachin

1

這對我有效。

myArray.sort(function (a, b) { 
    var a_s = a.substring(0, a.indexOf('ssss') + 4); 
    var a_n = a.substring(a.indexOf('ssss') + 4, a.indexOf('.txt')); 
    var b_s = b.substring(0, b.indexOf('ssss') + 4); 
    var b_n = b.substring(b.indexOf('ssss') + 4, b.indexOf('.txt')); 
    if (a_s < b_s) 
     return -1; 
    if (a_s > b_s) 
     return 1; 
    return parseInt(a_n) - parseInt(b_n); 
}); 

jsFiddle

+0

我試過這個,但我沒有得到確切的輸出..首先我需要排序日期然後按照'.txt'之前的數字。現在只按日期排序而不是數字 – sachin

+0

檢查最後的jsFiddle鏈接。右下方的面板顯示輸出,它按字符串和數字排序,這與日期和數字相同(只要日期和月份用'0'填充,例如'07'= 7月) – azz

+0

請將您的答案移動到http://stackoverflow.com/questions/19012468/javascript-sort-an-array-of-strings-that-c​​an-contain-letters-numbers-and-dates – hakre

0

按字符串內的數字值排序。

這是假設:

  • 數是整數
  • 每個字符串是不同
  • 日期可能會作爲數字排序(年,月,日)

["aa_123","aa_13","aa_2","aa_22_bb_23","aa_22_bb_3"].sort(function (a , b) {

var as = a.split(/([0-9]+)/); // splits string retaining separators 
    var bs = b.split(/([0-9]+)/); 
    var i,c = Math.min(as.length,bs.length); 
    for (i=0;i<c && as[i]===bs[i];++i) ; 
    var an = (i&1)?+as[i]:as[i]; // separators (digits) always at odd index 
    var bn = (i&1)?+bs[i]:bs[i]; 
    return (an<bn)?-1:1; // assumes every string different 

});

結果:

[ 
    "aa_2", 
    "aa_13", 
    "aa_22_bb_3", 
    "aa_22_bb_23", 
    "aa_123" 
] 
0

此,從給定串中提取的數字,並提出一個給定的重量上的每個數值的一部分。因此,您可以按照計數,日,月,年的優先順序對其進行排序。

function weightedNumSort(myArray,weightNum,weightString) {  
    var WEIGHTS_NUM = weightNum || [1,2,4,3]; //[YEAR,MONTH,DAY,COUNT], You can pass an array with appropriate weights for the number at the given position in the text, e.g year is the first Number 
    var WEIGHT_STRING = weightString || 1; //And a weight for the string value. If none get passed, default weights are used 

     function weightedSum (a,b,i) { 
      return (a + b * (WEIGHTS_NUM [i-1] || 1)); 
     } 

    myArray = myArray.slice().sort(function (a, b) { 
     var reg = /(\d+)/g //A regex to extract the numerical part 

     var lNum = a.match(reg) //Extract the numerical parts we now have an array ["2013","09","26","2"] 

     var rNum = b.match(reg) 

     var delta = Array.apply(null,{length:lNum.length+1}); 
      delta [0] = 0; //add a 0 at the beginning, for convenience with the reduce function 

     for (var i=0,j=lNum.length; i < j; i++) { 
      var value = lNum[i] - rNum[i]; 
      value = ~~ (value/Math.abs (value)) // 1 for positive values, 0 for 0 , -1 for negative values, to make weighting easier 
      delta[i+1] = value; 
     } 

     var weightedNumValue = delta.reduce (weightedSum) //Put a weight on the number parts. 
     var weightedStrValue = WEIGHT_STRING * (a > b ? 1 : a < b ? -1 : 0)  
     return weightedNumValue + weightedStrValue //Add the weighted values and we have a positive or negative value with a correct weight on the numerical parts 
    }) 
    return myArray 
} 

輸出

console.log (
    weightedNumSort (myArray)  
) /* 
[ 
    "_aaaa_2013-09-25_ssss5.txt", 
    "_aaaa_2013-09-25_ssss6.txt", 
    "_aaaa_2013-09-25_ssss7.txt", 
    "_aaaa_2013-09-25_ssss8.txt", 
    "_aaaa_2013-09-25_ssss9.txt", 
    "_aaaa_2013-09-25_ssss10.txt", 
    "_aaaa_2013-09-25_ssss13.txt", 
    "_aaaa_2013-09-26_ssss1.txt", 
    "_aaaa_2013-09-26_ssss2.txt" 
]*/ 

Fiddle

相關問題