问题
I wanted to see if it is possible to combine two strings and order them by Date/Time?
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
Now strCOMBO will give me both of the strings together but I need them to be sorted by date/time, maybe using the CDate function?
Thanks again everyone I really appreciate all of the help that you give me.
回答1:
Take a look at this quetion and using that, you can do something like this
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)
Results:
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###
Please note that you have to make sure that the string can be parsed using CDate function and results in a valid date or do whatever you have to when calling Call objSortedList.Add(CDate(Replace(arrCombo(i), "###", "")), arrCombo(i)) i.e. the first argument (Key) must be a valid date, if you want to sort by date.
回答2:
Just my version
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
来源:https://stackoverflow.com/questions/9382850/combine-two-strings-and-order-them-by-date-time