Combine Two Strings and Order them by Date/Time PA

2019-08-20 01:48发布

问题:

This is more of a 2nd part of a question that start off here here

The code below works great but I forgot something, there is also a City and State before the Date/Time, Example is:

strcountstf = "Chicago, IL,02/01/2012 3:05am###,Akron,OH,02/02/2012 7:05am###,Nashivlle,TN,02/05/2012 8:30pm###" 
strDateNTimes = "Buffalo,NY,03/01/2011 2:20am###,Las Vegas,NV,12/02/2012 8:00am###,Mount Vernon,IN,02/06/2012 6:45pm###"

Below is the working code but I forgot to add the City and State into strcountstf and strDateNTimes string. Now when the City,State is added it does not work.

I would like to sort it by Date/Time but show the City,State as the Output/Result... please help AGAIN!! (I know, I know).

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) 

回答1:

Make a Class with properties City, State and Time and a method to give a well shaped string back and fill them accordingly.

Class OmNomNom
    Public City, State
    Private time_, timeStr_
    Public Property Let [Time](t)
        timeStr_ = t
        time_ = cDate(replace(t, "###", ""))
    End Property
    Public Property Get [Time] ' as Date
        [Time] = time_
    End Property

    Public Function Regurgigate ' as String
        Regurgigate = join(array(City, State, timeStr_), ",")
    End Function

End Class

Set objSortedList = Server.CreateObject("System.Collections.SortedList")
For each chocolateCookie in array(strcountstf, trDateNTimes)
    for i = 0 to ubound(chocolateCookie) step 3
        Set myOmNomNom = new omNomNom
        myOmNomNom.City = chocolateCookie(i)
        myOmNomNom.State = chocolateCookie(i+1)
        myOmNomNom.Time = chocolateCookie(i+2)
        objSortedList.Add myOmNomNom.Time, myOmNomNom
    next
next

Now you can get your proper strings by iterating through the sorted list (as you did before) and get the string from the Regurgigate method: objSortedList.GetByIndex(i).Regurgigate()

Disclaimer: Sample code is not tested.



回答2:

You'd need to use the function split() to make an array of each of the strings, then loop through the array and create an entirely new array. If the string is longer than 2 characters (ie, not a state) and doesn't contain ### (ie, not a date) then load the first array value as "city". Consequently if it matches only 2 characters, it's a State (load it into the 2nd array value) and last but not least, if it has ### in it, you'll load it in the 3rd array value.

Ending up with something like:

Array(
[0] => 'City' => 'Buffalo', 'State' => 'NY', 'Date' => '03/01/2011 2:20am###'
[1] => 'City' => 'Chicago', 'State' => 'IL', 'Date' => '03/01/2011 2:20am###'
)

Once you have that you can adjust the sortedlist code you already have to accommodate the new fields and sort them.