Skip to main content Link Search Menu Expand Document (external link) Copy Copied

SortOnDisk

New

Sorts a CSV file on disk rather than in memory.


Syntax

expression.SortOnDisk(filePath, [SortingKeys:= 1], [Headers:= True], [streamSize := 20], [SortAlgorithm:= SortingAlgorithms.SA_Quicksort], [ExportationBunchSize:= 10000])

Parameters

Part Description
filePath Required. Identifier specifying a String Type variable representing the CSV file path, including file extension.
SortingKeys Optional. Identifier specifying a Variant Type variable representing the columns/keys for the logical comparisons.
Headers Optional. Identifier specifying a Boolean Type variable.
streamSize Optional. Identifier specifying a Single Type variable representing the single textstream factor size.
SortAlgorithm Optional. Identifier specifying a member of the SortingAlgorithms Enumeration.
ExportationBunchSize Optional. Identifier specifying a Long Type variable representing the amount of items to export in a single operation.

Returns value

Type: String

⚠️Caution

The sorting operation will require intensive I/O usage of the hard disk drive, so the performance of the method will also be tied to the R/W speed of the disk.

See also
CSVTextStream class, Sort method.

Behavior

The data sorting start on the second record when the Headers parameter is set to True. If the SortingKeys parameter is omitted the data will be sorted on the first column/field in ascending order, set this parameter to a negative Integer to sort the data in descending order on the given column (e.g. SortingKeys:=-2 will sort in descending order on the second field). In addition, the user can pass a one-dimensional array in the SortingKeys parameter to achieve multilevel data sorting on several fields at once. The returned string is the full path to the new sorted file whose name has the form “*-sorted.csv” where “*” represents the name of the CSV file to be sorted.

☕Example

Sub SortOnDisk()
    Dim CSVint As CSVinterface
    Dim SortKeys() As Long
    
    Set CSVint = New CSVinterface
    With CSVint.parseConfig
        .path = Environ("USERPROFILE") & "\Desktop\Demo_100000records.csv"
    End With
    ReDim SortKeys(0 To 2)
    SortKeys(0) = -1: SortKeys(1) = 5: SortKeys(2) = -11
    With CSVint
        .SortOnDisk .parseConfig.path, sortingKeys:=SortKeys    'Sort the data in descending order on column 1,
                                                                'then sort in ascending order on column 5 and
                                                                'sort in descending order on column 11. This
                                                                'multi-level sorting is "stable".
    End With
    Set CSVint = Nothing
End Sub

Back to Methods overview