ProductName  

Sorts the columns or rows of the current matrix object.

Syntax

MatrixObject.SortCols RowIndex, SortOrder, PermIndex
MatrixObject.SortRows ColIndex, SortOrder, PermIndex

Parameters

RowIndex
Optional. Indicates the row by which the matrix columns are to be sorted. Default value is 0 (first row).
ColIndex
Optional. Indicates the column by which the matrix rows are to be sorted. Default value is 0 (first column).
SortOrder
Optional. A SortOrderEnum value indicating the sort order.
PermIndex
Optional. A variant, that upon exit, will contain an array of Long integers and records the columns' or rows' dispositions.

The following table describes the possible values for the SortOrder parameter.

Constant Description Value
bmSortAscending (Default) Sort in ascending order. 0
bmSortDescending Sort in descending order. 1

 

Remarks

PermIndex can be used as an input for ReorderCols or ReorderRows method.

Error Codes

Error 1319 is returned if RowIndex exceeds matrix size.

Error 1320 is returned if ColIndex exceeds matrix size.

Example

This example demonstrates the use of the SortRows and ReorderRows method. Matrix A is sorted by its first column. The permutation index that results from SortRows method is used as input in ReorderRows method to reorder matrix B.
Private Sub MatrixSortReorderRows()

Dim A As Matrix, B As Matrix
Dim PI As Variant
    Set A = New Matrix
    A.Size 6, 6
    A.FillRandom
    
    Set B = New Matrix
    B.Size 6, 1
    B.FillRandom
    
    Debug.Print "A = "
    Debug.Print A.GetString
    Debug.Print "B = "
    Debug.Print B.GetString
    
    A.SortRows 0, msoAscending, PI
    B.ReorderRows PI

    Debug.Print "A sorted"
    Debug.Print A.GetString
    Debug.Print "B reordered"
    Debug.Print B.GetString
    
End Sub

Output

A = 
|  6.000  2.000  8.000  6.000  5.000  4.000 |
|  9.000  8.000  7.000  2.000  9.000  7.000 |
|  5.000  3.000  0.000  1.000  4.000  1.000 |
|  2.000 10.000  4.000  1.000  0.000  0.000 |
|  4.000  5.000  6.000  6.000  6.000  2.000 |
|  7.000  5.000  4.000  1.000  6.000  8.000 |

B = 
|  5.000 |
|  3.000 |
|  9.000 |
|  7.000 |
| 10.000 |
|  9.000 |

A sorted
|  2.000 10.000  4.000  1.000  0.000  0.000 |
|  4.000  5.000  6.000  6.000  6.000  2.000 |
|  5.000  3.000  0.000  1.000  4.000  1.000 |
|  6.000  2.000  8.000  6.000  5.000  4.000 |
|  7.000  5.000  4.000  1.000  6.000  8.000 |
|  9.000  8.000  7.000  2.000  9.000  7.000 |

B reordered
|  7.000 |
| 10.000 |
|  9.000 |
|  5.000 |
|  9.000 |
|  3.000 |

See Also

NormalizeMe Method

Applies To: Matrix | CMatrix