CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Sorting Months

  1. #1
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    Sorting Months

    Hi people, I'm sure I'm not the only one that ever needed to sort months in the right order and hope some of you can help me out.

    I have a string array holding the names of the months randomly and would like to manipulate the array so that it will hold the months in the right order.

    Muchas Gracias! (Thanks!)

    Go Raptors!


  2. #2
    Join Date
    May 2001
    Posts
    40

    Re: Sorting Months

    How about a 2 dimension array with something like this
    February | 2
    January | 1
    March | 3

    and then sort on the second field just a thought



  3. #3
    Join Date
    Sep 2000
    Posts
    6

    Re: Sorting Months

    Use the Format function Format(<table entry>,"mm") when you do your comparisons in the sort routine.

    If Format(table(1),"mm") > Format(table(2),"mm") then


    Dave



  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Sorting Months

    Ok, so I know this isn't the best, BUT I don't have a lot of time to do this up right. Place 3 Text boxes on a form, and name them "Text" (Make sure the indexes are 0,1,2) Place a command button on the form. Paste the following code into the form and run. It should sort the three months you type in.


    private Sub Command1_Click()
    Dim I as Integer
    Dim J as Integer
    Dim TT(2) as string
    Dim TT1 as string

    for I = 0 to 2
    TT(I) = text(I).text + " 1, 1999"
    next I
    for I = 0 to 1
    for J = I + 1 to 2
    If CDate(TT(I)) > CDate(TT(J)) then
    TT1 = TT(I)
    TT(I) = TT(J)
    TT(J) = TT1
    End If
    next J
    next I
    for I = 0 to 2
    text(I).text = Left(TT(I), InStr(1, TT(I), " ") - 1)
    next I

    End Sub






    Hope this helps.


  5. #5
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    Re: Sorting Months

    Thanks Dave Sharp, Z LoveLife, and especially sotoasty for your suggestions!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured