Array sort

Keywords: 
Situation: 

You want to sort data in an array. For a report, for example.

Actions: 

Use a proc in the explanation section of this pattern. Note: the array passed into the proc is processed in-place. Its original order is lost. If you want to keep the original order, make of copy of the array before you sort it.

Explanation: 

This proc will sort an array in-place. That is, the original order will be lost.

If you need to keep the original order, make a copy of the array before you sort it.

  1. Sub bubbleSort(ByRef arr, ByVal highIndex As Integer)
  2.   'https://social.msdn.microsoft.com/Forums/en-US/830b42cf-8c97-4aaf-b34b-d860773281f7/sorting-an-array-in-vba-without-excel-function?forum=isvvba
  3.   'With small changes.
  4.   Dim strTemp As String
  5.   Dim i As Long
  6.   Dim j As Long
  7.   Dim lngMin As Long
  8.   Dim lngMax As Long
  9.   lngMin = 1
  10.   lngMax = highIndex
  11.   For i = lngMin To lngMax - 1
  12.     For j = i + 1 To lngMax
  13.       If arr(i) > arr(j) Then
  14.         strTemp = arr(i)
  15.         arr(i) = arr(j)
  16.         arr(j) = strTemp
  17.       End If
  18.     Next j
  19.   Next i
  20. End Sub