# Thread: [RESOLVED] Sorting a DATE Array

1. ## [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. ## 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.

3. ## 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. ## 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.

5. ## Re: Sorting a DATE Array

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

6. ## 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. ## Re: Sorting a DATE Array

Why not store actual date values instead of formatted text?

8. ## 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. ## 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. ## 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