Imports System
Imports System.Collections

Namespace ASPNET.StarterKit.TimeTracker.BusinessLogicLayer
    '*********************************************************************
    '
    ' ProjectsCollection Class
    '
    ' ProjectsCollection class inherits from ArrayList.  It has its own implementation of Sort
    ' base on the sortable Project fields
    '
    '*********************************************************************
    Public Class ProjectsCollection
        Inherits ArrayList
        Public Enum ProjectFields
            InitValue
            Name
            ManagerUserName
            CompletionDate
            Duration
        End Enum 'ProjectFields


        Public Overloads Sub Sort(ByVal sortField As ProjectFields, ByVal isAscending As Boolean)
            Select Case sortField
                Case ProjectFields.Name
                    MyBase.Sort(New NameComparer())
                Case ProjectFields.ManagerUserName
                    MyBase.Sort(New ManagerUserNameComparer())
                Case ProjectFields.CompletionDate
                    MyBase.Sort(New CompletionDateComparer())
                Case ProjectFields.Duration
                    MyBase.Sort(New DurationComparer())
            End Select

            If Not isAscending Then
                MyBase.Reverse()
            End If
        End Sub 'Sort

        Private NotInheritable Class NameComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                Dim first As Project = CType(x, Project)
                Dim second As Project = CType(y, Project)
                Return first.Name.CompareTo(second.Name)
            End Function 'Compare
        End Class 'NameComparer

        Private NotInheritable Class ManagerUserNameComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                Dim first As Project = CType(x, Project)
                Dim second As Project = CType(y, Project)
                Return first.ManagerUserName.CompareTo(second.ManagerUserName)
            End Function 'Compare
        End Class 'ManagerUserNameComparer

        Private NotInheritable Class CompletionDateComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                Dim first As Project = CType(x, Project)
                Dim second As Project = CType(y, Project)
                Return first.EstCompletionDate.CompareTo(second.EstCompletionDate)
            End Function 'Compare
        End Class 'CompletionDateComparer

        Private NotInheritable Class DurationComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                Dim first As Project = CType(x, Project)
                Dim second As Project = CType(y, Project)
                Return first.EstDuration.CompareTo(second.EstDuration)
            End Function 'Compare
        End Class 'DurationComparer
    End Class 'ProjectsCollection
End Namespace 'ASPNET.StarterKit.TimeTracker.BusinessLogicLayer