Combine Two Strings and Order them by Date/Time

2019-07-27 08:23发布

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.

2条回答
时光不老,我们不散
2楼-- · 2019-07-27 08:29

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-27 08:40

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
查看更多
登录 后发表回答