Sorting a List View using a Custom Sort Routine

Provided by: Dan Haught, FMS Executive Vice President

This example shows how to respond to a column click event. The ColumnClick event fires when a column header is clicked, and is generally used for initiating sorts.

Example

To use this code, create a new Windows Form object and add a Listview control. Add the following code to the form’s class. Then hook up the ColumnClick event to the appropriate code (VB.NET or C#).

' VB

    Private Sub ListView1_ColumnClick( _

        ByVal sender As Object, _

        ByVal e As System.Windows.Forms.ColumnClickEventArgs) _

        Handles ListView1.ColumnClick

 

        ' Create a new instance of our Sorter class. Pass the number

        ' of the column that was clicked.

        Dim Sorter As ListViewSorter = _

            New ListViewSorter(e.Column, ListViewSorter.EnumSortOrder.Ascending)

 

        ' Tell the ListView to use the Sorter class

        ListView1.ListViewItemSorter = Sorter

 

    End Sub

 

    Public Class ListViewSorter

        ' We need to Implement IComparer so our

        ' ListView can use this class

        Implements System.Collections.IComparer

 

        Public Enum EnumSortOrder As Integer

            Ascending = 0

            Descending = 1

        End Enum

 

        Public SortOrder As EnumSortOrder

        Public SortColumn As Integer

 

        ' Default constructor

        '  SortColumn - ListView column to sort on

        Public Sub New( _

            ByVal SortColumn As Integer, _

            ByVal SortOrder As EnumSortOrder)

 

            Me.SortColumn = SortColumn

            Me.SortOrder = SortOrder

 

        End Sub

 

        ' Compare objects based in the IComparer Interface

        Public Function Compare( _

            ByVal x As Object, _

            ByVal y As Object) _

            As Integer _

            Implements System.Collections.IComparer.Compare

 

            Dim xString As String

            Dim YString As String

 

            ' Convert the two passed values to listview items

            Dim l1 As ListViewItem

            Dim l2 As ListViewItem

 

            l1 = CType(x, ListViewItem)

            l2 = CType(y, ListViewItem)

 

            ' Get the appropriate text values depending on whether we are being asked

            ' to sort on the first column (0) or subitem columns (>0)

            If SortColumn = 0 Then

                ' SortColumn is 0, we need to compare the
                ' Text property of the Item itself

                xString = l1.Text

                YString = l2.Text

            Else

                ' SortColumn is not 0, so we need to compare the Text
                ' property of the SubItem

                xString = l1.SubItems(SortColumn).ToString

                YString = l2.SubItems(SortColumn).ToString

            End If

 

            ' Do the comparison

            If xString = YString Then

                ' Values are equal

                Return 0

            ElseIf xString > YString Then

                ' X is greater than Y

                If SortOrder = EnumSortOrder.Ascending Then

                    Return 1

                Else

                    Return -1

                End If

            ElseIf xString < YString Then

                ' Y is greater than X

                If SortOrder = EnumSortOrder.Ascending Then

                    Return -1

                Else

                    Return 1

                End If

 

            End If

 

        End Function

 

    End Class

 

 

// C#

 

private void HandleColumnClick (

object sender,

ColumnClickEventArgs e)

{

    // Create a new instance of our Sorter class. Pass the number

    // of the column that was clicked.

    ListViewComparer sorter = new ListViewComparer (e.Column, ViewComparer.EnumSortOrder.Descending);

     // Tell the ListView to use the Sorter class

    ListView1.ListViewItemSorter = sorter;

}

 public class ListViewComparer : IComparer

{

    // We need to Implement IComparer so our ListView can use this class

    // Define sort orders

    public enum EnumSortOrder: int { Ascending = 0, Descending = 1    }

    public ListViewComparer (int WhichSortColumn, EnumSortOrder WhichOrder)

    {

        this.SortColumn = WhichSortColumn;

        this.SortOrder = WhichOrder;

    }

     public EnumSortOrder SortOrder ;

    public int SortColumn;

 

    // Compare objects based in the IComparer Interface

    public int Compare( object item1, object item2 )

    {

        string xString;

        string yString;

 

        // Convert the two passed values to listview items

        ListViewItem l1 = (ListViewItem) item1;

        ListViewItem l2 = (ListViewItem) item2;

 

        // Get the appropriate text values depending on whether we are being asked

        // to sort on the first column (0) or subitem columns (>0)

        if (SortColumn ==0)

        {

            // SortColumn is 0, we need to compare the Text
            // property of the Item itself

            xString = l1.Text;

            yString = l2.Text;

        }

        else

        {

            // SortColumn is not 0, so we need to compare the
            // Text property of the SubItem

            xString = l1.SubItems[SortColumn].Text.ToString();

            yString = l2.SubItems[SortColumn].Text.ToString();

        }

 

        // Do the comparison

        if (string.Compare (xString, yString) == 0)

        {

            // Values are equal

            return 0;

        }

        else if (string.Compare(xString, yString) > 0)

        {

            // X is greater than Y

            if (SortOrder == EnumSortOrder.Ascending)

            return 1;

            else

            return -1;

        }

        else if (string.Compare(xString, yString) < 0)

        {   

            // Y is greater than X

            if  (SortOrder == EnumSortOrder.Ascending)

            return -1;

            else

            return 1;

        }

        else

            return 0;

        }

    }


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS