Sorting a String Array

btisdabomb

Member
Joined
Jul 12, 2006
Messages
16
Programming Experience
Beginner
I have a sting array that I need to sort in a certain order. The array can contain but may not always contain each of the following...

CoverPage
TOC
ReportName1
...
ReportName10

These items are in the array in random order. I need to sort them in the order listed above. The array might only contain 3 items (Reportname6, CoverPage, ReportName 9) but it will still need to be sorted in the above order for each item that exists in the array.

Hopefully that makes sense... If not I can try and explain better.

How would I go about doing that?
 
If I understand what your saying then I think I have a solution.

Take the array and pass it a custom class implementing IComparer

VB.NET:
Public Class ChapterCompare
    Implements IComparer

    Private ReadOnly StrHeader() As String = {"CoverPage", "TOC"}
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim strX, strY As String
        strX = CStr(x)
        strY = CStr(y)

        If strX = strY Then Return 0

        Dim XIndex, YIndex As Integer
        XIndex = Array.IndexOf(StrHeader, strX)
        YIndex = Array.IndexOf(StrHeader, strY)

        If XIndex <> -1 And (YIndex = -1 OrElse XIndex < YIndex) Then Return -1
        If YIndex <> -1 And (XIndex = -1 OrElse YIndex < XIndex) Then Return 1
        
        Dim intX, intY As Integer
        intX = CInt(strX.Replace("ReportName", ""))
        intY = CInt(strY.Replace("ReportName", ""))

        Return intX.CompareTo(intY)
    End Function
End Class

The following code shows how to use it.

VB.NET:
        Dim StrItems() As String = {"ReportName3", "ReportName1", "CoverPage", "TOC", "ReportName2", "ReportName10"}
        Dim strList As String = ""

        For Each Item As String In StrItems
            strList &= Item & vbCrLf
        Next

        MsgBox(strList)
        Array.Sort(StrItems, New ChapterCompare)

        strList = ""

        For Each Item As String In StrItems
            strList &= Item & vbCrLf
        Next

        MsgBox(strList)

I setup the IComparer class so that you can add additional items later.

I will say it was a quick (20 minutes) implementation, So I'll leave the clean up coding to you. For example some error handling for invalid items in the array (Like if you had Index or AppendixA)

Let me know if this is what you were looking for.
 
Last edited:
Implements IComparer
Get used to Generics and better strongly typed code from .Net 2 and up. Use the IComparer(Of T), in this case IComparer(Of String).

You could also use the Comparison(Of T) delegate function, this is simply a function with two arguments of same type that return an integer, same results as the IComparer.Compare method. But instead of writing one class for each sort (or adding property variations) you can add these methods to the typed class they should sort (or outside) to provide ready alternate sorts. These methods you can supply to the Sort method with AddressOf.

The third option used to provide the default sort order of a class is the IComparable(Of T), this have the CompareTo method and is used internally in the class to compare current instance to another of same type. If you Sort a collection/array with items of this type without specifying any other compare parameter it will use this interface implementation.

In this case the String class is not written by you (and can't be inherited and overridden) so IComparable(Of T) is not an option, but you could use both a IComparer(Of String) class and the Comparison(Of T) delegate with about the same code as the Object version - just a little safer, tidier, more readable and with explicit intention.
 
Back
Top