Sort Method

Applies To

Column object, Range object, Selection object, Table object.

Description

Syntax 1: Sorts the specified table column.

Syntax 2: Sorts the specified table.

Syntax 3: Sorts the paragraphs in the specified range or selection.

Syntax 1

expression.Sort(ExcludeHeader, SortFieldType, SortOrder, CaseSensitive, LanguageID)

Syntax 2

expression.Sort(ExcludeHeader, FieldNumber, SortFieldType, SortOrder, FieldNumber2,
ú SortFieldType2, SortOrder2, FieldNumber3, SortFieldType3, SortOrder3, CaseSensitive,
ú LanguageID)

Syntax 3

expression.Sort(ExcludeHeader, FieldNumber, SortFieldType, SortOrder, FieldNumber2,
ú SortFieldType2, SortOrder2, FieldNumber3, SortFieldType3, SortOrder3, SortColumn,
ú Separator, CaseSensitive, LanguageID)

expression Syntax 1: Required. An expression that returns a Column object.

Syntax 2: Required. An expression that returns a Table object.

Syntax 3: Required. An expression that returns a Range or Selection object.

ExcludeHeader Optional Variant. True to exclude the first row or paragraph from the sort operation. The default value is False.

FieldNumber, FieldNumber2, FieldNumber3 Optional Variant. The fields to sort by. Word sorts by FieldNumber, then by FieldNumber2, and then by FieldNumber3.

SortFieldType, SortFieldType2, SortFieldType3 Optional Variant. The column sort type (Syntax 1) or the respective sort types for FieldNumber, FieldNumber2, and FieldNumber3 (Syntax 2 and Syntax 3). Can be one of the following WdSortFieldType constants: wdSortFieldAlphanumeric, wdSortFieldDate, or wdSortFieldNumeric. The default value is wdSortFieldAlphanumeric.

SortOrder, SortOrder2, SortOrder3 Optional Variant. The column sorting order (Syntax 1) or the sorting order to use when sorting FieldNumber, FieldNumber2, and FieldNumber3 (Syntax 2 and Syntax 3). Can be one of the following WdSortOrder constants: wdSortOrderAscending or wdSortOrderDescending. The default value is wdSortAscending.

SortColumn Optional Variant. True to sort only the column specified by the Range or Selection object.

Separator Optional Variant. The type of field separator. Can be one of the following WdSortSeparator constants: wdSortSeparateByCommas, wdSortSeparateByDefaultTableSeparator, or wdSortSeparateByTabs. The default value is wdSortSeparateByCommas. If the range or selection is in a table, this argument is ignored.

CaseSensitive Optional Variant. True to sort with case sensitivity. The default value is False.

LanguageID Optional Variant. Specifies the sorting language. Can be one of the WdLanguageID constants.

Remarks

If you want to sort paragraphs within a table cell, include only the paragraphs and not the end-of-cell mark; if you include the end-of-cell mark in a selection or range and then attempt to sort the paragraphs, Word displays a message stating that it found no valid records to sort.

See Also

SortAscending method, SortDescending method.

Example

This example creates a 5x5 table in a new document, inserts text into each cell, and then sorts the table in descending alphanumeric order.

Set newDoc = Documents.Add 
Set myTable = newDoc.Tables.Add(Selection.Range, 5, 5)
For iRow = 1 To myTable.Rows.Count
    For iCol = 1 To myTable.Columns.Count
        Set myRange= myTable.Rows(iRow).Cells(iCol).Range
        myRange.InsertAfter "Cell (" & Str$(iRow) & "," & Str$(iCol) & ")"
    Next iCol
Next iRow
MsgBox "Click OK to sort in descending order."
myTable.Sort SortOrder:=wdSortOrderDescending
This example inserts three lines of text into a new document and then sorts the lines in ascending alphanumeric order.

Set newDoc = Documents.Add 
newDoc.Content.InsertAfter "pear" & Chr(13) & "zucchini" & Chr(13) _ 
    & "apple" & Chr(13)
newDoc.Content.Sort SortOrder:=wdSortOrderAscending