Results 1 to 7 of 7

Thread: [RESOLVED] List of Time Zones in right order

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] List of Time Zones in right order

    Hello, I am trying to list in a combo-box (like in the Time Zone tab of the "Date and Time Properties" of windows).

    I am able to this using this code:

    http://vbnet.mvps.org/index.html?cod...onedisplay.htm

    However the results are not in order. Anybody know how to sort them?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List of Time Zones in right order

    If you sort it by Display Name or Std Basis, it will be.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: List of Time Zones in right order

    what order do you want them in? i have code to sort them by gmt offset
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: List of Time Zones in right order

    Hello Westconn,

    I want it in the same order as in Windows Which is by GMT as in -12,-11...0...11, 12

    Thanks for your assistance.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: List of Time Zones in right order

    this is the code that was given to me to sort the array of timezones and i am using
    VB Code:
    1. Private Sub SortArray(ByRef sArray() As String)
    2. Dim i         As Long
    3. Dim ArrTemp() As Long
    4.  
    5.     ReDim ArrTemp(UBound(sArray))
    6.     For i = LBound(sArray) To UBound(sArray)
    7.         ArrTemp(i) = Val(Trim$(Mid(sArray(i), 5, 6)))
    8.     Next
    9.     BubbleSortNumbers ArrTemp, sArray
    10. End Sub
    11.  
    12. Private Sub BubbleSortNumbers(varray() As Long, vRealarray() As String)
    13.    
    14.    Dim cnt1     As Long
    15.    Dim cnt2     As Long
    16.    Dim tmp      As Long
    17.    Dim tmpStr   As String
    18.          For cnt1 = UBound(varray) To LBound(varray) Step -1
    19.  
    20.       For cnt2 = LBound(varray) + 1 To cnt1
    21.      
    22.       If varray(cnt2 - 1) > varray(cnt2) Then
    23.          'Switch Positions array
    24.          tmp = varray(cnt2 - 1)
    25.          varray(cnt2 - 1) = varray(cnt2)
    26.          varray(cnt2) = tmp
    27.          'Switch real array
    28.          tmpStr = vRealarray(cnt2 - 1)
    29.          vRealarray(cnt2 - 1) = vRealarray(cnt2)
    30.          vRealarray(cnt2) = tmpStr
    31.       End If
    32.             Next cnt2
    33.  
    34.    Next cnt1
    35. End Sub
    36.  
    37. ' call like this
    38.         opentimezonelist  ' sub to return array of timezones
    39.        
    40.         Dim arrsort() As String
    41.         ReDim arrsort(UBound(arrdisplay))  ' array of returned timezones
    42.         arrsort = arrdisplay
    43.        
    44.         SortArray arrsort
    45.         For i = 0 To UBound(arrsort)
    46.             List1.AddItem arrsort(i)
    47.         Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: List of Time Zones in right order

    Thanks westconn1 works perfectly.

  7. #7

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: [RESOLVED] List of Time Zones in right order

    Hello westconn1, I added a small piece of code to your code so after the items are sorted using GMT, I do a secondary alphabetical sort. All I did was add an extra condition in the BubbleSortNumbers function where the GMT are identical. Here is the code:

    VB Code:
    1. Private Sub SortArray(ByRef strArray() As String)
    2.  
    3. Dim iCounter As Integer
    4. Dim lngArrayTmp() As Long
    5.  
    6. On Error Resume Next
    7.  
    8. ReDim lngArrayTmp(UBound(strArray))
    9.  
    10. For iCounter = LBound(strArray) To UBound(strArray)
    11.     lngArrayTmp(iCounter) = Val(Trim$(Mid$(strArray(iCounter), 5, 6)))
    12. Next
    13.  
    14. Call BubbleSortNumbers(lngArrayTmp, strArray)
    15.  
    16. End Sub
    17.  
    18. Private Sub BubbleSortNumbers(lngArray() As Long, strArray() As String)
    19.    
    20. Dim iCounter As Integer
    21. Dim jCounter As Integer
    22. Dim lngTmp As Long
    23. Dim strTmp As String
    24.  
    25. On Error Resume Next
    26.  
    27. For iCounter = UBound(lngArray) To LBound(lngArray) Step -1
    28.     For jCounter = (LBound(lngArray) + 1) To iCounter
    29.         If lngArray(jCounter - 1) > lngArray(jCounter) Then
    30.             'Switch Positions array
    31.             lngTmp = lngArray(jCounter - 1)
    32.             lngArray(jCounter - 1) = lngArray(jCounter)
    33.             lngArray(jCounter) = lngTmp
    34.             'Switch real array
    35.             strTmp = strArray(jCounter - 1)
    36.             strArray(jCounter - 1) = strArray(jCounter)
    37.             strArray(jCounter) = strTmp
    38.         ElseIf lngArray(jCounter - 1) = lngArray(jCounter) Then
    39.             If StrComp(strArray(jCounter - 1), strArray(jCounter), vbTextCompare) = 1 Then
    40.                 'Switch Positions array
    41.                 lngTmp = lngArray(jCounter - 1)
    42.                 lngArray(jCounter - 1) = lngArray(jCounter)
    43.                 lngArray(jCounter) = lngTmp
    44.                 'Switch real array
    45.                 strTmp = strArray(jCounter - 1)
    46.                 strArray(jCounter - 1) = strArray(jCounter)
    47.                 strArray(jCounter) = strTmp
    48.             End If
    49.         End If
    50.     Next jCounter
    51. Next iCounter
    52.    
    53. End Sub

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