Share via


ListObject.Sort Property

Definition

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

public:
 property Microsoft::Office::Interop::Excel::Sort ^ Sort { Microsoft::Office::Interop::Excel::Sort ^ get(); };
public Microsoft.Office.Interop.Excel.Sort Sort { get; }
member this.Sort : Microsoft.Office.Interop.Excel.Sort
Public ReadOnly Property Sort As Sort

Property Value

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 void SortListObject()
{
    // Create ListObject control (table) and set table style
    Microsoft.Office.Tools.Excel.ListObject employeeTable =
        this.Controls.AddListObject(this.Range["A1"], 
        "employeeTable");

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

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

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

}
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

Applies to