1

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.

compcobalt
  • 1,322
  • 7
  • 31
  • 61
  • If you put your dates in UTC format you can simply sort them by ASCII order – Diodeus - James MacFarlane Feb 21 '12 at 18:18
  • 1
    This is missing something. Your strCOMBO is just ONE concatenated string. How are we going to sort just ONE concatenated string? Are you feeding an array of these things? Do they need to be split by commas, concatenated and then sorted? Do you want strcountstf and strDatesNTimes to be sorted FIRST and THEN concatenated? A little bit more information, if you please. – Carlos Feb 21 '12 at 18:21
  • @mastashake57 both strcountstf and strDateNTimes are arrays, one is from an ms access database and the other is from an sql database. I can and will have them ordered when I query them/open/call the table. now I'm ending up with 2 strings that I want to combine and then order them by date/time so kinda connect stringA with stringB and then order by date/time. I hope this makes more sence, thanks for the question mastashake57 I hope I made it more clear – compcobalt Feb 21 '12 at 19:42
  • Is this related to your question from yesterday? Did you try and create a view in Access to combine your results? – RogueSpear00 Feb 21 '12 at 20:13

2 Answers2

5

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.

Community
  • 1
  • 1
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • I think you are on the right track I just cannot get it to sort correctly, can you look into it again please ? – compcobalt Feb 21 '12 at 19:45
  • @Diodeus but how can I combine them and then sort them once they are combined ? – compcobalt Feb 21 '12 at 19:52
  • @compcobalt: Seems to work fine for me. What locale would your script be running? Your date formats will be sensitive to the locale. – AnthonyWJones Feb 21 '12 at 20:05
  • @AnthonyWJones what is the first date returend for you ? is it 03/01/2011 2:20am### ? everything tooks right except for the 1st date? (Note: the month is the 3rd (03)) – compcobalt Feb 21 '12 at 20:08
  • 1
    @compcobalt, I updated the comment with what I got using en-US locale. You have to make sure that the Key is a valid date. – amit_g Feb 21 '12 at 20:15
  • @compcobalt: Here in the UK that date is the 3rd of Jan 2011 (which BTW is the only date in 2011 so it should always be first regardless of the order of month/day portion) perhaps 2011 is a typo? – AnthonyWJones Feb 21 '12 at 20:20
  • @AnthonyWJones, Thanks for the note, I added a 2011 date to make sure that the sorting was indeed working correctly in the test as all other dates were Feb 2012. – amit_g Feb 21 '12 at 20:27
0

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
Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
  • 1
    Well that does work with the string data given but only because in this case the individual strings will compare with `>` the same as they would as the dates they represent. However if one of the strings were "02/02/2012 2:00pm" it would fail since lexically this string would comes before "02/02/2012 8:00am" however as a date type it would come after. – AnthonyWJones Feb 21 '12 at 21:12
  • PART#2 of the same question [here](http://stackoverflow.com/questions/9386486/combine-two-strings-and-order-them-by-date-time-part-2) – compcobalt Feb 21 '12 at 22:45