组合两个字符串并按日期/时间排序

时间:2012-02-21 18:14:46

标签: asp-classic vbscript

我想看看是否可以组合两个字符串并按日期/时间排序?

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函数?

再次感谢大家,我非常感谢你给予我的所有帮助。

2 个答案:

答案 0 :(得分: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))时执行任何操作,即第一个参数(Key)必须是有效的日期,如果您想按日期排序。

答案 1 :(得分: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