Sorting an ArrayList

lkrterp

Member
Joined
Jul 10, 2007
Messages
21
Programming Experience
Beginner
Hi All!

I am trying to sort an arraylist using .Sort() but am not sure what to put in the () for the field/column I want to sort on. Could use some help. Here is the portion of my code that creates the array from a SQL Query

VB.NET:
Dim th As TktHoldList
                    Dim aTktHold As ArrayList = New ArrayList
                    For i = 0 To dt.Rows.Count - 1
                    th = New TktHoldList
                    th.CustGroup = (dt.Rows(i).Item(0)).ToString.Trim
                    th.CustNbr = (dt.Rows(i).Item(1)).ToString.Trim
                    th.CustName = (dt.Rows(i).Item(2)).ToString.Trim & ", " & (dt.Rows(i).Item(3)).ToString.Trim
                    th.Site = (dt.Rows(i).Item(4)).ToString.Trim
                    th.FillDD = (dt.Rows(i).Item(5)).ToString.Trim
                    th.OutDD = (dt.Rows(i).Item(6)).ToString.Trim
                    th.DelvInst1 = ""
                    th.DelvInst2 = ""
                    th.CaseStatus = (dt.Rows(i).Item(7)).ToString.Trim
                    Try
                        th.NxtDelv = (dt.Rows(i).Item(8)).ToShortDateString
                    Catch
                        th.NxtDelv = ""
                    End Try
                    th.CustTerms = (dt.Rows(i).Item(9)).ToString.Trim
                    th.CustStatus = (dt.Rows(i).Item(10)).ToString.Trim
                    Try
                        th.RunOut = (dt.Rows(i).Item(11)).ToShortDateString
                    Catch
                        th.RunOut = ""
                    End Try

                    custnbr = (dt.Rows(i).Item(1)).ToString.Trim

                    th.PastDueAmt = f.GetPastDueBalance(custnbr)

                    aTktHold.Add(th)

                Next
            End If

            [B]aTktHold.Sort("[U]NOT SURE WHAT TO PUT HERE[/U]" )[/B]           
            Session("THold") = aTktHold
            gvTicketHold.DataSource = aTktHold

I want to sort by the CustGroup field, but am not sure what to put in the () after the sort to do that.

I will then be binding the ArrayList to a Datagrid.

Thanks for any help.
 
By default the Sort method will only use the default comparison of the items in the collection. If you want to provide a custom comparison then you do so by defining your own class that implements the IComparer interface. For instance, consider a Person type that has a Name field and a DateOfBirth field. Let's assume that you may want to sort a collection of such items by either field. You need to define an IComparer class that will do that for you:
VB.NET:
Public Class PersonComparer
    Implements IComparer

    Private fieldName As String

    Public Sub New(ByVal fieldName As String)
        Me.fieldName = fieldName
    End Sub

    Public Function Compare(ByVal x As Object, _
                            ByVal y As Object) As Integer Implements IComparer.Compare
        Dim result As Integer

        Select Case Me.fieldName
            Case "Name"
                'Compare by Name.
                result = DirectCast(x, Person).Name.CompareTo(DirectCast(y, Person).Name)
            Case "DateOfBirth"
                'Compare by DateOfBirth.
                result = DirectCast(x, Person).DateOfBirth.CompareTo(DirectCast(y, Person).DateOfBirth)
            Case Else
                'Don't compare.
                result = 0
        End Select

        Return result
    End Function

End Class
The standard behaviour for comparisons is to return a value less than zero if the first item comes first, zero if they are equivalent and a value greater than zero if the second item comes first. The CompareTo methods of the native VB data types will provide such values for you.

Now, to use this class, you create an instance and pass it to the Sort method, e.g.
VB.NET:
myArrayList.Sort(New PersonComparer("Name"))
That will sort myArrayList by Name. If you wanted to sort by DateOfBirth then you'd specify that field name when creating the PersonComparer.

Note that this is just an example of how the IComparer interface can be implemented. The specifics are up to you and depend on the situation. All that's required is a Compare method that takes two Objects and returns an Integer. You should endeavour to use the CompareTo methods of the inbuilt VB types as much as you can though, as it tends to make your code as clear as possible.
 
Back
Top