Free code

Sort a Multidimensional Array with VB.Net

5 years ago I wrote this article on how to sort a multidimensional array with ASP. This morning I was contacted by James Philips of  Djanogly City Academy Nottingham, who told me that he found my article, and has adapted the function for VB.NET compilation. Additionally, he has modified it to allow sorting on numeric criteria so that numbers aren’t sorted as strings.

With his permission I'm publishing the function. Enjoy!


Function fSortArray(ByVal arToSort As Array, ByVal sortBy As Integer, ByVal compareDates As Boolean, ByVal IsNumericData As Boolean) As Array
 
        Dim c As Integer
        Dim d As Integer
        Dim e As Integer
        Dim smallestValue As String
        Dim smallestIndex As String
        Dim tempValue As String
 
        For c = 0 To UBound(arToSort, 2)
 
            smallestValue = arToSort(sortBy, c)
            smallestIndex = c
 
            For d = c + 1 To UBound(arToSort, 2)
                If Not compareDates Then
                    If IsNumericData = True Then
                        If CInt(arToSort(sortBy, d)) > CInt(smallestValue) Then
                            smallestValue = arToSort(sortBy, d)
                            smallestIndex = d
                        End If
                    Else
                        If StrComp(arToSort(sortBy, d), smallestValue) < 0 Then
                            smallestValue = arToSort(sortBy, d)
                            smallestIndex = d
                        End If
                    End If
                Else
                    If Not IsDate(smallestValue) Then
                        fSortArray = fSortArray(arToSort, sortBy, False, IsNumericData)
                        Exit Function
                    Else
                        If DateDiff("d", arToSort(sortBy, d), smallestValue) > 0 Then
                            smallestValue = arToSort(sortBy, d)
                            smallestIndex = d
                        End If
                    End If
                End If
            Next
            If smallestIndex <> c Then 'swap
                For e = 0 To UBound(arToSort, 1)
                    tempValue = arToSort(e, smallestIndex)
                    arToSort(e, smallestIndex) = arToSort(e, c)
                    arToSort(e, c) = tempValue
                Next
            End If
        Next
 
        fSortArray = arToSort
 
    End Function
Share

2 Comments

  1. I created an array with 10 rows, 5 elements per row and ran this code. It does not work properly as it did not sort anything, it just jumbled up the array values and returned them. For example, element 1 became element 4, element 3 became element 2… and so on.

    Can I suggest that when you post code like this you clearly explain the calling usage of the function. For instance, your "SortBy" value is unclear and no matter what I changed it to, I still got the same incorrect unsorted results.

  2. I had to change the case with the DateDiff to :

    If Date.Compare(arToSort(sortBy, d), smallestValue) < 0 Then
    smallestValue = arToSort(sortBy, d)
    smallestIndex = d
    End If

    This worked to sort by date.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image