2012-02-21 107 views
1

我想查看是否可以合併兩個字符串並按日期/時間排序?合併兩個字符串並按日期/時間排序

dim strcountstf 
dim strDateNTimes 
dim strCOMBO 
strcountstf = "02/01/2012 3:05am###,02/02/2012 7:05am###,02/05/2012 8:30pm###" 
strDateNTimes = "02/01/2012 2:20am###,02/02/2012 8:00am###,02/06/2012 6:45pm###" 

strCOMBO = strcountstf & strDateNTimes 

現在strCOMBO會給我兩個字符串在一起,但我需要他們按日期/時間排序,也許使用CDate功能?

再次感謝大家,我非常感謝您給我的所有幫助。

+0

如果你把在UTC格式的日期,你可以簡單地通過ASCII爲了 – 2012-02-21 18:18:21

+1

這是失去了一些東西對它們進行排序。你的strCOMBO只是一個串聯的字符串。我們如何排序一個串聯的字符串?你在喂這些東西嗎?他們是否需要用逗號分割,連接然後排序?你想strcountstf和strDatesNTimes排序第一和然後連接?如果您願意,請提供一點信息。 – Carlos 2012-02-21 18:21:53

+0

@ mastashake57 strcountstf和strDateNTimes都是數組,一個來自ms訪問數據庫,另一個來自sql數據庫。當我查詢/打開/調用表格時,我可以並且會讓他們訂購。現在我想要結合2個字符串,然後按日期/時間對它們進行排序,所以請將stringA與stringB連接,然後按日期/時間排序。我希望這使得更多的意見,感謝問題mastashake57我希望我更清楚 – compcobalt 2012-02-21 19:42:49

回答

5

看看this quetion和使用,你可以做這樣的事情

dim strcountstf 
dim strDateNTimes 
dim strCOMBO 
dim arrCOMBO 
dim strCOMBOSorted 
dim objSortedList 
dim i 

strcountstf = "02/01/2012 3:05am###,02/02/2012 7:05am###,02/05/2012 8:30pm###" 
strDateNTimes = "03/01/2011 2:20am###,02/02/2012 8:00am###,02/06/2012 6:45pm###" 

strCOMBO = strcountstf & "," & strDateNTimes 

arrCombo = Split(strCOMBO, ",") 

Set objSortedList = Server.CreateObject("System.Collections.SortedList") 

For i = LBound(arrCombo) To UBound(arrCombo) 
    Call objSortedList.Add(CDate(Replace(arrCombo(i), "###", "")), arrCombo(i)) 
Next 

strCOMBOSorted = "" 

For i = 0 To objSortedList.Count - 1 
    strCOMBOSorted = strCOMBOSorted & ", " & objSortedList.GetByIndex(i) 
Next 

strCOMBOSorted = Right(strCOMBOSorted, Len(strCOMBOSorted) - 2) 

Set objSortedList = Nothing 

Response.Write("<br>") 
Response.Write(strCOMBO) 
Response.Write("<br>") 
Response.Write(strCOMBOSorted) 

結果:

02/01/2012 3:05am###,02/02/2012 7:05am###,02/05/2012 8:30pm###,03/01/2011 2:20am###,02/02/2012 8:00am###,02/06/2012 6:45pm### 
03/01/2011 2:20am###, 02/01/2012 3:05am###, 02/02/2012 7:05am###, 02/02/2012 8:00am###, 02/05/2012 8:30pm###, 02/06/2012 6:45pm### 

請注意,您必須確保該字符串可以使用解析CDate功能,併產生一個有效的日期或做任何你必須調用Call objSortedList.Add(CDate(Replace(arrCombo(i), "###", "")), arrCombo(i)),即第一個參數(鍵)必須是有效的日期,如果你想按日期排序做任何事情。

+0

我認爲你走在正確的軌道上我無法正確地分類,你能再看一遍嗎? – compcobalt 2012-02-21 19:45:15

+0

@Diodeus,但我怎麼能結合他們,然後將它們分類後,他們合併? – compcobalt 2012-02-21 19:52:30

+0

@compcobalt:似乎爲我工作得很好。你的腳本將運行什麼區域?你的日期格式對區域設置很敏感。 – AnthonyWJones 2012-02-21 20:05:13

0

只是我的版本

Option Explicit 

    Dim strcountstf, strDateNTimes, strCOMBO, strArr, ans, a, j, temp 


    strcountstf = "02/01/2012 3:05am###,02/02/2012 7:05am###,02/05/2012 8:30pm###" 
    strDateNTimes = "02/01/2012 2:20am###,02/02/2012 8:00am###,02/06/2012 6:45pm###" 

    strCOMBO = strcountstf &","& strDateNTimes 


    strArr = Split(strCOMBO,",") 


    for a = UBound(strArr) - 1 To 0 Step -1 
     for j= 0 to a 
     if strArr(j)>strArr(j+1) then 
      temp=strArr(j+1) 
      strArr(j+1)=strArr(j) 
      strArr(j)=temp 
     end if 
    next 
next 

For a =0 to UBound(strArr) 
    ans= ans &","& strArr(a) 
Next 
ans= Right(ans,Len(ans)-1) 
MsgBox ans 
+1

那麼,它可以處理給定的字符串數據,但只能因爲在這種情況下,單個字符串將與'>'進行比較,就像它們代表的日期一樣。然而,如果其中一個字符串是「2012年2月2日下午2:00」,它會失敗,因爲詞法上這個字符串會在「2012年2月2日上午8:00」之前出現,但是會作爲日期類型出現。 – AnthonyWJones 2012-02-21 21:12:20

+0

相同問題的PART#2 [here](http:// stackoverflow。com/questions/9386486/combine-two-strings-and-order-them-by-date-time-part-2) – compcobalt 2012-02-21 22:45:14

+0

@ AnthonyWJones:謝謝:)從我的代碼(+1) – Amol 2012-02-22 04:23:03

相關問題