How to Use the QuickSort Algorithm to Sort MS Flex Grid

How to Use the QuickSort Algorithm to Sort MS Flex Grid

Using this code will rewrite all the rows in MS Flex Grid and it supports alphanumeric as well as numeric sorts for specified columns.
Good Luck!

 Option ExplicitPublic Sub QSort(Grid As MSFlexGrid, ByVal Column As Integer, ByVal min AsLong, _    ByVal max As Long, ByVal Ascending As Boolean, ByVal NumComp As Boolean)    Dim tmp() ' when swap rows keep copy here    ReDim tmp(Grid.Cols)    Dim med_value, hi As Long, lo As Long, i As Integer    If min >= max Then Exit Sub    med_value = Grid.TextMatrix(min, Column)    SaveRow Grid, min, tmp    lo = min    hi = max    Do        Do While Compare(Grid.TextMatrix(hi, Column), med_value, NumComp, Ascending) >= 0            hi = hi - 1            If hi = hi Then Exit Do        Loop        If lo >= hi Then            lo = hi            RestoreRow Grid, hi, tmp            Exit Do        End If        For i = 0 To Grid.Cols - 1            Grid.TextMatrix(hi, i) = Grid.TextMatrix(lo, i)        Next i    Loop    QSort Grid, Column, min, lo - 1, Ascending, NumComp    QSort Grid, Column, lo + 1, max, Ascending, NumCompEnd SubPrivate Function Compare(ByVal X, ByVal Y, ByVal NumComp As Boolean, _    ByVal Ascending As Boolean) As Integer    Dim b As Integer    If NumComp Then        X = CDbl(X)        Y = CDbl(Y)    End If    If X > Y Then b = 1    If X 


Share the Post: