Share via


ListObject.Sort Property

Gets the sort column or columns, and the sort order for the ListObject collection.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Syntax

'Declaration
ReadOnly Property Sort As Sort
    Get
Sort Sort { get; }

Property Value

Type: Microsoft.Office.Interop.Excel.Sort
A Microsoft.Office.Interop.Excel.Sort that represents the sort column or columns, and the sort order for the ListObject collection.

Examples

The following code example adds a ListObject to the current worksheet. The example then populates the ListObject, which corresponds to an Excel table, with two rows of arbitrary data and specifies that the sort is to be performed in ascending order based on the column range A1:A3. Next, the example calls the Microsoft.Office.Interop.Excel.Sort.Apply method to sort the table.

This example is for a document-level customization.

Private Sub SortListObject()
    ' Create ListObject control (table) and set table style
    Dim employeeTable As Microsoft.Office.Tools.Excel.ListObject = _
        Me.Controls.AddListObject(Me.Range("A1"), "employeeTable")

    ' Populate table with some data
    Dim rng As Excel.Range
    rng = employeeTable.InsertRowRange
    rng(ColumnIndex:=1).Value2 = "bb"
    rng(ColumnIndex:=2).Value2 = "b1"
    Dim row2 As Excel.ListRow = employeeTable.ListRows.AddEx( _
        AlwaysInsert:=True)
    rng = row2.Range
    rng(ColumnIndex:=1).Value2 = "aa"
    rng(ColumnIndex:=2).Value2 = "a1"

    ' Set sort properties        
    employeeTable.Sort.SortFields.Add(Me.Range("A1", "A3"), _
        Excel.XlSortOn.xlSortOnValues, Excel.XlSortOrder.xlAscending)

    ' Sort worksheet
    employeeTable.Sort.Apply()

End Sub
private void SortListObject()
{
    // Create ListObject control (table) and set table style
    Microsoft.Office.Tools.Excel.ListObject employeeTable =
        this.Controls.AddListObject(this.Range["A1",missing], 
        "employeeTable");

    // Populate table with some data
    Excel.Range rng;
    rng = employeeTable.InsertRowRange;
    ((Excel.Range)rng[missing,1]).Value2 = "bb";
    ((Excel.Range)rng[missing,2]).Value2 = "b1";
    Excel.ListRow row2 = employeeTable.ListRows.AddEx(
        missing, true);
    rng = row2.Range;
    ((Excel.Range)rng[missing,1]).Value2 = "aa";
    ((Excel.Range)rng[missing,2]).Value2 = "a1";

    // Set sort properties        
    employeeTable.Sort.SortFields.Add(this.Range["A1", "A3"],
        Excel.XlSortOn.xlSortOnValues, 
        Excel.XlSortOrder.xlAscending, missing, missing);

    // Sort worksheet
    employeeTable.Sort.Apply();

}

.NET Framework Security

See Also

Reference

ListObject Interface

Microsoft.Office.Tools.Excel Namespace