sorting an array

mtaylor314

Member
Joined
Nov 30, 2006
Messages
19
Location
Cleves, OH
Programming Experience
Beginner
I am writing a for/next loop to go through an ArrayList. Each object in the array contain a FromDate, ToDate, and Location. I need to sort out each object first by ToDate, in ascending order, then by FromDate, in ascending order. The kicker is that if both dates match in an object, that when I print the Location to the file that should be under the same From-To.

FromDate to ToDate
Location 1
Location 2
FromDate to ToDate
Location 3
FromDate to ToDate
Location 4
Location 5
Location 6

I would appreciate any help in writhing the pseudo code. I know it will invole if statements, but my brain is fried. Please help.
 
I create two classes; travel and travelcompare:
VB.NET:
    Class travel
        Public fromdate, todate As Date, location As String
    End Class
 
    Class travelcompare
        Implements IComparer(Of travel)
 
        Public Function Compare(ByVal x As travel, ByVal y As travel) As Integer _
        Implements System.Collections.Generic.IComparer(Of travel).Compare
            If x.todate <> y.todate Then
                Return x.todate < y.todate
            ElseIf x.fromdate <> y.fromdate Then
                Return x.fromdate < y.fromdate
            Else
                Return x.location < y.location
            End If
        End Function
    End Class
Then I create a list of travel, add some data, sort it and present it:
(I use the List(Of T) instead of your ArrayList since you're on .Net 2.0)
VB.NET:
    Sub express()
        Dim l As New List(Of travel)
        addsometravels(l)
        l.Sort(New travelcompare)
        present(l)
    End Sub
The first helper method create some test data:
VB.NET:
    Sub addsometravels(ByRef l As List(Of travel))
        Dim t As travel
        t = New travel
        t.fromdate = New Date(2006, 12, 1)
        t.todate = New Date(2006, 12, 13)
        t.location = "home"
        l.Add(t)
        t = New travel
        t.fromdate = New Date(2006, 12, 1)
        t.todate = New Date(2006, 12, 13)
        t.location = "away"
        l.Add(t)
        t = New travel
        t.fromdate = New Date(2006, 11, 1)
        t.todate = New Date(2006, 12, 11)
        t.location = "here"
        l.Add(t)
        t = New travel
        t.fromdate = New Date(2006, 10, 1)
        t.todate = New Date(2006, 12, 1)
        t.location = "there"
        l.Add(t)
    End Sub
The second helper method present the data:
VB.NET:
    Sub present(ByVal l As List(Of travel))
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To l.Count - 1
            If i = 0 OrElse _
                (l.Item(i).todate <> l.Item(i - 1).todate Or _
                 l.Item(i).fromdate <> l.Item(i - 1).fromdate) Then
                sb.AppendFormat("from {0} to {1}", _
                                    l.Item(i).fromdate.ToShortDateString, _
                                    l.Item(i).todate.ToShortDateString)
                sb.AppendLine()
            End If
            sb.AppendFormat("location: {0}", l.Item(i).location)
            sb.AppendLine()
        Next
        MsgBox(sb.ToString)
    End Sub
Hope the example is useful.
 
Back
Top