dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] Sorting a DATE Array

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,238

    Resolved [RESOLVED] Sorting a DATE Array

    Using this simple routine to sort a 2D array, I can sort the first element (Which is a date (mm/dd/yyyy format))...but, it sorts the first element as if it were a string (that is, ex: 1/2/2009. 10/3/2019. 3/3/2010)
    What am I doing incorrectly?

    Code:
    Dim myArray(2,50) As Variant
    
    SortArray myArray, 0
    
    Public Sub SortArray(ByRef DArray(), Element As Integer)
    
        Dim gap As Integer, doneflag As Integer, SwapArray()
        Dim Index As Integer, acol As Integer, CNT As Integer
        ReDim SwapArray(2, UBound(DArray, 1), UBound(DArray, 2))
        gap = Int(UBound(DArray, 2) / 2)
        Do While gap >= 1
            Do
                doneflag = 1
                For Index = 0 To (UBound(DArray, 2) - (gap + 1))
                    If DArray(Element, Index) > DArray(Element, (Index + gap)) Then
                        For acol = 0 To (UBound(SwapArray, 2) - 1)
                            SwapArray(0, acol, Index) = DArray(acol, Index)
                            SwapArray(1, acol, Index) = DArray(acol, Index + gap)
                        Next
                        For acol = 0 To (UBound(SwapArray, 2) - 1)
                            DArray(acol, Index) = SwapArray(1, acol, Index)
                            DArray(acol, Index + gap) = SwapArray(0, acol, Index)
                        Next
                        CNT = CNT + 1
                        doneflag = 0
                    End If
                Next
            Loop Until doneflag = 1
            gap = Int(gap / 2)
        Loop
    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,554

    Re: Sorting a DATE Array

    Dumb question... how are you adding the dates to the array? Is it possible your sorting function is flawed? Recommend passing a small array, just 3 or so dates and walk it (step by step) and see what's happening.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    24,634

    Re: Sorting a DATE Array

    as you are sorting variant arrays there is no reason for it to sort as date values, you could try

    Code:
    If CDate(DArray(Element, Index)) > CDate(DArray(Element, (Index + gap))) Then

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,846

    Re: Sorting a DATE Array

    Yes, that would be my thought as well.

    As another option, you could break in the array loop and do some TypeName(...) checking of the date values, for instance:

    Code:
                    Debug.Print TypeName(DArray(Element, Index)), TypeName(DArray(Element, (Index + gap)))
                    If DArray(Element, Index) > DArray(Element, (Index + gap)) Then
    That will tell you whether or not you're actually comparing two dates. If you're actually comparing dates, things should work fine. If you're comparing strings of dates, that won't work very well at all unless those strings are very well organized as dates (such as yyyymmdd).

    Also, I didn't think through your sort algorithm, but I suspect it's something simple, as in how you're putting these dates into the array.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    24,634

    Re: Sorting a DATE Array

    look in the locals window to see the variable types contained in the variant array

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,720

    Re: Sorting a DATE Array

    Date-time values are landmines if you aren't vigilant about avoiding implicit coercion to/from String data. This was probably the motivation behind disabling it in VB.Net via Option Strict.

    It feels like we spend an awful lot of threads on this one issue yet the lesson never gets learned.

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,622

    Re: Sorting a DATE Array

    Why not store actual date values instead of formatted text?

  8. #8

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,238

    Re: Sorting a DATE Array

    Ah crap!...appears I am putting those 'dates' in as Strings.
    I have a date/time field in an Access Table, but instead of putting that information directly into the array, I had first sent it to a listbox (for other reasons) and split two fields.
    Then I took that split data and put it into the array...hence, strings.
    I'm going to fix that and hopefully all will be well.

    @Arnoutdv.....I want to sort by birth dates for display in a grid, as well as then exporting that grid data to excel to pass on to church leaders.

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,622

    Re: Sorting a DATE Array

    The first sort the original data on the birthday field, then fill the grid.
    While filling the grid you can format the date fields to whatever you like.

  10. #10

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,238

    Re: [RESOLVED] Sorting a DATE Array

    Done...was simple once I was prompted to check HOW I was putting the date information into the array. Some days!!!!!!!!

    Thanks all.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width