What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse an IComparer class to sort a file using the values in its columns
DescriptionThis example shows how to use an IComparer class to sort a file using the values in its columns. The IComparer class compares values in two rows' columns to see which should come before the other.
Keywordssort, IComparer, file, compare, Visual Basic .NET
CategoriesAlgorithms, Software Engineering
The Array class's Sort method can sort just about anything if you tell it how to compare two objects of the type you're sorting. The idea is to make a class that implements IComparer and pass an instance to Array.Sort so it knows how to sort the lines in the file.

The ColumnSorter class implements IComparer(Of String) so it can compare two strings. The constructor takes a list of the columns that should be used to compare two lines from the file. If a column index is negative, that means the sorter should sort the column in descending order.

The really interesting part of the class is the Compare function, which compares two rows from the file. In this case, it splits the lines into space-separated fields and compares the correct column values.

Public Class ColumnSorter
    Implements IComparer(Of String)

    Private m_Columns() As Integer

    ' Save the column numbers for sorting.
    Public Sub New(ByVal ParamArray numbers() As Integer)
        m_Columns = numbers
    End Sub

    ' Compare based on the columns.
    Public Function Compare(ByVal x As String, ByVal y As _
        String) As Integer Implements _
        System.Collections.Generic.IComparer(Of _
        ' Split the strings into fields.
        Dim x_fields() As String = x.Split(New String() {" " & _
            ""}, StringSplitOptions.RemoveEmptyEntries)
        Dim y_fields() As String = y.Split(New String() {" " & _
            ""}, StringSplitOptions.RemoveEmptyEntries)

        ' Compare the columns.
        For i As Integer = 0 To m_Columns.GetUpperBound(0)
            Dim col As Integer = m_Columns(i)
            If col < 0 Then
                ' Sort descending.
                col = -col
                If x_fields(col) < y_fields(col) Then _
                    Return 1
                If x_fields(col) > y_fields(col) Then _
                    Return -1
                ' Sort ascending.
                If x_fields(col) < y_fields(col) Then _
                    Return -1
                If x_fields(col) > y_fields(col) Then _
                    Return 1
            End If
        Next i
        Return 0
    End Function
End Class
Once you've built the class that implements IComparer, using it is easy. The following code reads the file's lines into a String array, makes a ColumnSorter to sort on the second column descending and the third column ascending (column indexes start from 0), calls Array.Sort, and writes the result into the output file.
Private Sub btnSort_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSort.Click
    ' Read the input file.
    Dim lines() As String = _

    ' Make a column sorter.
    ' Note: Columns start numbering from 0.
    Dim column_comparer As New ColumnSorter(-1, 2)

    ' Sort.
    Array.Sort(lines, column_comparer)

    ' Save the result.
    System.IO.File.WriteAllLines(txtOutputFile.Text, lines)

End Sub
The IComparer class is very flexible and you can use it to sort practically anything from the simple strings in this example to complex objects. For example, you could use IComparer classes to sort Employee objects based on first and last name, employee ID, or Social Security number.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.