Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3996558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:17:38+00:00 2026-05-20T07:17:38+00:00

I have defined the following Array Dim myArray(10,5) as Long and would like to

  • 0

I have defined the following Array Dim myArray(10,5) as Long and would like to sort it. What would be the best method to do that?

I will need to handle a lot of data like a 1000 x 5 Matrix. It contains mainly numbers and dates and need to sort it according to a certain column

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:17:39+00:00Added an answer on May 20, 2026 at 7:17 am

    Here’s a multi-column and a single-column QuickSort for VBA, modified from a code sample posted by Jim Rech on Usenet.

    Notes:

    You’ll notice that I do a lot more defensive coding than you’ll see in most of the code samples out there on the web: this is an Excel forum, and you’ve got to anticipate nulls and empty values… Or nested arrays and objects in arrays if your source array comes from (say) a third-party realtime market data source.

    Empty values and invalid items are sent to the end of the list.

    To sort multi-column arrays, your call will be:

     QuickSortArray MyArray,,,2

    ...Passing '2' as the column to sort on and excluding the optional parameters that pass the upper and lower bounds of the search domain.

    Sorting single-column arrays (vectors), instead use:

    QuickSortVector Myarray

    Here too excluding the optional parameters.

    [EDITED] - fixed an odd formatting glitch in the <code> tags, which seem to have a problem with hyperlinks in code comments.

    The Hyperlink I excised was Detecting an Array Variant in VBA.

    Public Sub QuickSortArray(ByRef SortArray As Variant, Optional lngMin As Long = -1, Optional lngMax As Long = -1, Optional lngColumn As Long = 0)
        On Error Resume Next
    
        'Sort a 2-Dimensional array
    
        ' SampleUsage: sort arrData by the contents of column 3
        '
        '   QuickSortArray arrData, , , 3
    
        '
        'Posted by Jim Rech 10/20/98 Excel.Programming
    
        'Modifications, Nigel Heffernan:
    
        '       ' Escape failed comparison with empty variant
        '       ' Defensive coding: check inputs
    
        Dim i As Long
        Dim j As Long
        Dim varMid As Variant
        Dim arrRowTemp As Variant
        Dim lngColTemp As Long
    
        If IsEmpty(SortArray) Then
            Exit Sub
        End If
        If InStr(TypeName(SortArray), "()") < 1 Then  'IsArray() is somewhat broken: Look for brackets in the type name
            Exit Sub
        End If
        If lngMin = -1 Then
            lngMin = LBound(SortArray, 1)
        End If
        If lngMax = -1 Then
            lngMax = UBound(SortArray, 1)
        End If
        If lngMin >= lngMax Then    ' no sorting required
            Exit Sub
        End If
    
        i = lngMin
        j = lngMax
    
        varMid = Empty
        varMid = SortArray((lngMin + lngMax) \ 2, lngColumn)
    
        ' We  send 'Empty' and invalid data items to the end of the list:
        If IsObject(varMid) Then  ' note that we don't check isObject(SortArray(n)) - varMid *might* pick up a valid default member or property
            i = lngMax
            j = lngMin
        ElseIf IsEmpty(varMid) Then
            i = lngMax
            j = lngMin
        ElseIf IsNull(varMid) Then
            i = lngMax
            j = lngMin
        ElseIf varMid = "" Then
            i = lngMax
            j = lngMin
        ElseIf VarType(varMid) = vbError Then
            i = lngMax
            j = lngMin
        ElseIf VarType(varMid) > 17 Then
            i = lngMax
            j = lngMin
        End If
    
        While i <= j
            While SortArray(i, lngColumn) < varMid And i < lngMax
                i = i + 1
            Wend
            While varMid < SortArray(j, lngColumn) And j > lngMin
                j = j - 1
            Wend
    
            If i <= j Then
                ' Swap the rows
                ReDim arrRowTemp(LBound(SortArray, 2) To UBound(SortArray, 2))
                For lngColTemp = LBound(SortArray, 2) To UBound(SortArray, 2)
                    arrRowTemp(lngColTemp) = SortArray(i, lngColTemp)
                    SortArray(i, lngColTemp) = SortArray(j, lngColTemp)
                    SortArray(j, lngColTemp) = arrRowTemp(lngColTemp)
                Next lngColTemp
                Erase arrRowTemp
    
                i = i + 1
                j = j - 1
            End If
        Wend
    
        If (lngMin < j) Then Call QuickSortArray(SortArray, lngMin, j, lngColumn)
        If (i < lngMax) Then Call QuickSortArray(SortArray, i, lngMax, lngColumn)
        
    End Sub
    

    ... And the single-column array version:

    Public Sub QuickSortVector(ByRef SortArray As Variant, Optional lngMin As Long = -1, Optional lngMax As Long = -1)
        On Error Resume Next
    
        'Sort a 1-Dimensional array
    
        ' SampleUsage: sort arrData
        '
        '   QuickSortVector arrData
    
        '
        ' Originally posted by Jim Rech 10/20/98 Excel.Programming
    
    
        ' Modifications, Nigel Heffernan:
        '       ' Escape failed comparison with an empty variant in the array
        '       ' Defensive coding: check inputs
    
        Dim i As Long
        Dim j As Long
        Dim varMid As Variant
        Dim varX As Variant
    
        If IsEmpty(SortArray) Then
            Exit Sub
        End If
        If InStr(TypeName(SortArray), "()") < 1 Then  'IsArray() is somewhat broken: Look for brackets in the type name
            Exit Sub
        End If
        If lngMin = -1 Then
            lngMin = LBound(SortArray)
        End If
        If lngMax = -1 Then
            lngMax = UBound(SortArray)
        End If
        If lngMin >= lngMax Then    ' no sorting required
            Exit Sub
        End If
    
        i = lngMin
        j = lngMax
    
        varMid = Empty
        varMid = SortArray((lngMin + lngMax) \ 2)
    
        ' We  send 'Empty' and invalid data items to the end of the list:
        If IsObject(varMid) Then  ' note that we don't check isObject(SortArray(n)) - varMid *might* pick up a default member or property
            i = lngMax
            j = lngMin
        ElseIf IsEmpty(varMid) Then
            i = lngMax
            j = lngMin
        ElseIf IsNull(varMid) Then
            i = lngMax
            j = lngMin
        ElseIf varMid = "" Then
            i = lngMax
            j = lngMin
        ElseIf VarType(varMid) = vbError Then
            i = lngMax
            j = lngMin
        ElseIf VarType(varMid) > 17 Then
            i = lngMax
            j = lngMin
        End If
    
        While i <= j
    
            While SortArray(i) < varMid And i < lngMax
                i = i + 1
            Wend
            While varMid < SortArray(j) And j > lngMin
                j = j - 1
            Wend
    
            If i <= j Then
                ' Swap the item
                varX = SortArray(i)
                SortArray(i) = SortArray(j)
                SortArray(j) = varX
    
                i = i + 1
                j = j - 1
            End If
    
        Wend
    
        If (lngMin < j) Then Call QuickSortVector(SortArray, lngMin, j)
        If (i < lngMax) Then Call QuickSortVector(SortArray, i, lngMax)
    
    End Sub
    

    I used to use BubbleSort for this kind of thing, but it slows down, severely, after the array exceeds 1024 rows. I include the code below for your reference: please note that I haven't provided source code for ArrayDimensions, so this will not compile for you unless you refactor it - or split it out into 'Array' and 'vector' versions.

    Public Sub BubbleSort(ByRef InputArray, Optional SortColumn As Integer = 0, Optional Descending As Boolean = False)
    ' Sort a 1- or 2-Dimensional array.
    
    Dim iFirstRow   As Integer
    Dim iLastRow    As Integer
    Dim iFirstCol   As Integer
    Dim iLastCol    As Integer
    Dim i           As Integer
    Dim j           As Integer
    Dim k           As Integer
    Dim varTemp     As Variant
    Dim OutputArray As Variant
    
    Dim iDimensions As Integer
    
    iDimensions = ArrayDimensions(InputArray)
    
        Select Case iDimensions
        Case 1
    
            iFirstRow = LBound(InputArray)
            iLastRow = UBound(InputArray)
            
            For i = iFirstRow To iLastRow - 1
                For j = i + 1 To iLastRow
                    If InputArray(i) > InputArray(j) Then
                        varTemp = InputArray(j)
                        InputArray(j) = InputArray(i)
                        InputArray(i) = varTemp
                    End If
                Next j
            Next i
            
        Case 2
    
            iFirstRow = LBound(InputArray, 1)
            iLastRow = UBound(InputArray, 1)
            
            iFirstCol = LBound(InputArray, 2)
            iLastCol = UBound(InputArray, 2)
            
            If SortColumn  InputArray(j, SortColumn) Then
                        For k = iFirstCol To iLastCol
                            varTemp = InputArray(j, k)
                            InputArray(j, k) = InputArray(i, k)
                            InputArray(i, k) = varTemp
                        Next k
                    End If
                Next j
            Next i
    
        End Select
            
    
        If Descending Then
        
            OutputArray = InputArray
            
            For i = LBound(InputArray, 1) To UBound(InputArray, 1)
            
                k = 1 + UBound(InputArray, 1) - i
                For j = LBound(InputArray, 2) To UBound(InputArray, 2)
                    InputArray(i, j) = OutputArray(k, j)
                Next j
            Next i
     
            Erase OutputArray
            
        End If
    
    End Sub
    

    This answer may have arrived a bit late to solve your problem when you needed to, but other people will pick it up when they Google for answers for similar problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following members defined in a class that I'm trying to deserialise:
I have defined a personalizable property on a web part and I would like
I have the following route defined routes.MapRoute( ItemName, {controller}/{action}/{projectName}/{name}, new { controller = Home,
So I have the following user defined type in my oracle database: CREATE OR
I have the following situation: A user will define a certain filter on a
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following preprocessor divective: #ifndef NDEBUG #define TRACE printf #else #define TRACE(...)
I have defined a custom Sharepoint list for special attributes related to a software
I have defined an interface in C++, i.e. a class containing only pure virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.