Combine Two Strings and Order them by Date/Time

我怕爱的太早我们不能终老 提交于 2019-12-11 08:00:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!