合并两个字符串并按日期/时间排序 [英] Combine Two Strings and Order them by Date/Time
问题描述
我想看看是否可以合并两个字符串并按日期/时间排序?
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
现在strCOMBO会将两个字符串都给我,但是我需要按日期/时间对它们进行排序,也许可以使用CDate函数?
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.
推荐答案
看看此问题并使用它,您可以执行以下操作
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)
结果:
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))
时必须执行的任何操作,即第一个参数(键)必须为有效日期,如果您想按日期排序.
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.
这篇关于合并两个字符串并按日期/时间排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!