|
| 1 | +--- |
| 2 | +title: SortByField |
| 3 | +parent: Methods |
| 4 | +grand_parent: API |
| 5 | +nav_order: 28 |
| 6 | +--- |
| 7 | + |
| 8 | +# SortByField |
| 9 | +{: .fs-6 } |
| 10 | + |
| 11 | +Sorts the imported CSV/TSV data. |
| 12 | +{: .fs-4 .fw-300 } |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Syntax |
| 17 | + |
| 18 | +*expression*.`SortByField`*(\[fromIndex:= -1\], \[toIndex:= -1\], \[SortingKey:= 1\], \[SortAlgorithm:= SortingAlgorithms.SA_IntroSort\])* |
| 19 | + |
| 20 | +### Parameters |
| 21 | + |
| 22 | +<table> |
| 23 | +<thead> |
| 24 | +<tr> |
| 25 | +<th style="text-align: left;">Part</th> |
| 26 | +<th style="text-align: left;">Description</th> |
| 27 | +</tr> |
| 28 | +</thead> |
| 29 | +<tbody> |
| 30 | +<tr> |
| 31 | +<td style="text-align: left;"><em>fromIndex</em></td> |
| 32 | +<td style="text-align: left;">Optional. Identifier specifying a <code>Long</code> Type variable.</td> |
| 33 | +</tr> |
| 34 | +<tr> |
| 35 | +<td style="text-align: left;"><em>toIndex</em></td> |
| 36 | +<td style="text-align: left;">Optional. Identifier specifying a <code>Long</code> Type variable.</td> |
| 37 | +</tr> |
| 38 | +<tr> |
| 39 | +<td style="text-align: left;"><em>SortingKey</em></td> |
| 40 | +<td style="text-align: left;">Optional. Identifier specifying a <code>Variant</code> Type variable.</td> |
| 41 | +</tr> |
| 42 | +<tr> |
| 43 | +<td style="text-align: left;"><em>SortAlgorithm</em></td> |
| 44 | +<td style="text-align: left;">Optional. Identifier specifying a member of the <code>SortingAlgorithms</code> Enumeration.</td> |
| 45 | +</tr> |
| 46 | +</tbody> |
| 47 | +</table> |
| 48 | + |
| 49 | +### Returns value |
| 50 | + |
| 51 | +*Type*: `CSVinterface` |
| 52 | + |
| 53 | +>📝**Note** |
| 54 | +>{: .text-grey-lt-000 .bg-green-000 } |
| 55 | +>Before sort data, is required to make a call to the `ImportFromCSV` or `ImportFromCSVstring` method. |
| 56 | +{: .text-grey-dk-300 .bg-grey-lt-000 } |
| 57 | + |
| 58 | +See also |
| 59 | +: [ImportFromCSV method](https://ws-garcia.github.io/VBA-CSV-interface/api/methods/importfromcsv.html), [ImportFromCSVstring method](https://ws-garcia.github.io/VBA-CSV-interface/api/methods/importfromcsvstring.html), [CSVArrayList class](https://ws-garcia.github.io/VBA-CSV-interface/api/csvarraylist.html), [Sort method](https://ws-garcia.github.io/VBA-CSV-interface/api/methods/sort.html). |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Behavior |
| 64 | + |
| 65 | +When the *fromIndex* parameter is omitted, the sorting of the data starts at the first field of the record specified in the *SortingKey* parameter. Omitting the *toIndex* parameter takes the sorting to the last available field. If the *SortingKey* parameter is omitted, the data will be sorted on the first field in ascending order, set this parameter to a negative `Integer` to sort the data in descending order on the given record (e.g. `SortingKey:=-2` will sort in descending order on the second record). In addition, the user can pass a one-dimensional array in the *SortingKey* parameter to achieve multilevel data sorting on several fields at once. |
| 66 | + |
| 67 | +### ☕Example |
| 68 | + |
| 69 | +```vb |
| 70 | +Sub SortByField() |
| 71 | + Dim CSVint As CSVinterface |
| 72 | + |
| 73 | + Set CSVint = New CSVinterface |
| 74 | + With CSVint.parseConfig |
| 75 | + .path = Environ("USERPROFILE") & "\Desktop\Demo_100000records.csv" |
| 76 | + End With |
| 77 | + With CSVint |
| 78 | + .ImportFromCSV .parseConfig |
| 79 | + .SortByField SortingKey:=1, SortAlgorithm:=SA_Quicksort 'Sort the data in ascending order on header record. |
| 80 | + 'The operation will change fields order instead |
| 81 | + 'of records ordering. |
| 82 | + End With |
| 83 | + Set CSVint = Nothing |
| 84 | +End Sub |
| 85 | +``` |
| 86 | + |
| 87 | +[Back to Methods overview](https://ws-garcia.github.io/VBA-CSV-interface/api/methods/) |
0 commit comments