Thursday, August 26, 2010

Case-insensitive sorting of Firebird field in a Visual Basic web application

I wanted to sort elements in a ListBox alphabetically without regard to case. However, Firebird does a case-sensitive sort, and I couldn't get it to recognize UPPER() in the SELECT statement.

One solutions is to add the rows in the table adapter data table to an array of elements of a custom class. The custom class implements IComparable, so when you do Array.sort you can get the case-insensitive order desired. Here's my custom class:

Public Class trendpoint
    Implements IComparable

    Public name As String
    Public description As String

    Public Sub New(ByVal name As String, ByVal description As String)
        Me.name = name
        Me.description = description
    End Sub

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

        Dim comparepoint As trendpoint = CType(obj, trendpoint)

        If Me.name.ToUpper < comparepoint.name.ToUpper Then
            Return -1
        ElseIf Me.name.ToUpper > comparepoint.name.ToUpper Then
            Return 1
        Else
            Return 0
        End If

    End Function

End Class

No comments: