0

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) 
Community
  • 1
  • 1
compcobalt
  • 1,322
  • 7
  • 31
  • 61

2 Answers2

2

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.

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • this is great!!! I will have to change some code to make it work for me but this is a great example... Thank you so much, now i understand how to get it to work... – compcobalt Feb 23 '12 at 16:18
1

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.

user85569
  • 394
  • 3
  • 10
  • 2
    I'm sorry I've explained it quite thoroughly and logically. You, as a programmer, should be able to make this work within 10 minutes. I'm not in the habit of posting code. Sorry. – user85569 Feb 21 '12 at 23:02
  • also logically I do understand: using logic I can answer 70-80% of questions here but using knowledge maybe 1-2%. I do understand your Logic: They are all seperated by a comma so you can just split them up, create an new array and then sortlist but I cannot get it to work. but never the less thank you for your answer and reply. – compcobalt Feb 21 '12 at 23:25
  • 1
    @compcobalt: FYI, Stackoverflow is a Programmer-to-Programmer Q&A site. There is a reasonable assumption that those asking questions have some programming competency and that _all_ questioners _are_ programmers. It is true that some aren't but you should not be surprised to be treated as one. As a general rule the "please write my code for me because I haven't got the foggiest" sort of questions don't go down too well here. Having said that I and others have answered one or two of such questions so there is no harm in trying, just try to hide the fact your are not a programmer. – AnthonyWJones Feb 22 '12 at 12:57
  • @AnthonyWJones you do have a valid point I come from EE so I gotta adjust alil' ;-)... Thx for everything... – compcobalt Feb 23 '12 at 16:20