Sorting a collection using IComparable?

kittyrube

Member
Joined
Jan 11, 2008
Messages
5
Programming Experience
1-3
Hi,

Really hope someone can help me with this as I am pretty new to it all.
I need to sort a collection cdtAssignment() by a datetime element (dtAssignmentMadeDate) in ascending order. I have included the structure of this below.
Any help would be greatly appreciated :)

HTML:
<s:complexType name="ArrayOfCdtAssignment">
<s:sequence>
 	 <s:element minOccurs="0" maxOccurs="unbounded" name="tAssignment" type="s1:cdtAssignment" /> 
  </s:sequence>
  </s:complexType>

<s:complexType name="cdtAssignment">
<s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="sAssignmentHeader" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sMOI" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="tLocationAddr" type="s1:cdtAddress" /> 
  <s:element minOccurs="0" maxOccurs="unbounded" name="tLocationPhone" type="s1:cdtPhone" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sStatus" type="s:string" /> 
  <s:element minOccurs="1" maxOccurs="1" name="dtAssignmentDate" type="s:dateTime" /> 
  <s:element minOccurs="1" maxOccurs="1" name="dtAssignmentMadeDate" type="s:dateTime" /> 
  <s:element minOccurs="0" maxOccurs="1" name="tAdjuster" type="s1:cdtAdjuster" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sSLA" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="unbounded" name="sDirections" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sArrivalWindow" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sDuration" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sAssignmentType" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="sLocationName" type="s:string" /> 
  <s:element minOccurs="1" maxOccurs="1" name="iSortIndex" type="s:int" /> 
  </s:sequence>
  </s:complexType>
 
Last edited by a moderator:
Sorting a collection using IComparable

If IComparable is not the way to go then any way this can be sorted by the datetime field would be great :D
 
You have posted a schema, how do you work with this? In some cases you may change/extend the class it becomes/represent, here you Implement IComparable and fill in the comparison code in the generated CompareTo method. In other cases you just create a new class for sorting and implement a IComparer (fill in the Compare method). Perhaps strongly typed IComparable(Of T) is more appropriate. Where does the data come from? What is the desired output? Maybe there are also other options depending on if the sorting is to take place in a UI control. One example of a basic structure and strongly typed IComparable is here:
VB.NET:
Structure cdtAssignment
    Implements IComparable(Of cdtAssignment) 'pressing (Enter) here generates the interface members

    Public dtAssignmentMadeDate As Date

    Public Function CompareTo(ByVal other As cdtAssignment) As Integer _
    Implements System.IComparable(Of cdtAssignment).CompareTo
        Return Me.dtAssignmentMadeDate < other.dtAssignmentMadeDate
    End Function
End Structure
 
Hi John, I am working on a UI to where the data is returned from a web service.
Currently i am formatting and displaying the data on a UI but I need to sort the instances of cdtAssignment by the datetime field
dtAssignmentMadeDate, i dont need to change the structure of the output, it should just be as cdtAssignment, except ordered by date ascending

I hope this makes sense :S

Thanks
 
Currently they are sorted as below - they are only inclided if created in 2008. Now I just want to sort them by date ascending, regardless of when created. I dont know if this helps or not.

VB.NET:
    Public Shared Function SortAssignments(ByVal tInput As cdtAssignment()) As cdtAssignment()

          Dim tOutput() As cdtAssignment = New cdtAssignment() {}

          Dim assignment As cdtAssignment

          If Not tInput Is Nothing Then
                For Each assignment In tInput
                    If assignment.dtAssignmentDate > "2008-01-01T00:00:00" Then
                        ReDim Preserve tOutput(tOutput.GetUpperBound(0) + 1)
                        tOutput(tOutput.GetUpperBound(0)) = New cdtAssignment
                        tOutput(tOutput.GetUpperBound(0)) = assignment
                        Exit For
                    End If
                Next
          End If

          Return tOutput

    End Function
 
Last edited by a moderator:
Hi John, I am working on a UI to where the data is returned from a web service.
Currently i am formatting and displaying the data on a UI

In that case the return type is defined in the generated proxy class. You can extend that class as mentioned with IComparable to provide default sorting. Example:
VB.NET:
Namespace localhost 
    Partial Class cdtAssignment
        Implements IComparable

        Public Function CompareTo(ByVal obj As Object) As Integer _
        Implements System.IComparable.CompareTo
            Return Me.dtAssignmentDate < DirectCast(obj, cdtAssignment).dtAssignmentDate 
        End Function
    End Class
End Namespace
This would sort with a call to Array.Sort(tInput). Use same namespace as your webservice namespace, see node in "Web References" in Solution Explorer.

Also you could write an independent sorter class implementing IComparer, example:
VB.NET:
Class sorter
    Implements IComparer(Of localhost.cdtAssignment)

    Public Function Compare(ByVal x As localhost.cdtAssignment, ByVal y As localhost.cdtAssignment) As Integer _
    Implements System.Collections.Generic.IComparer(Of localhost.cdtAssignment).Compare
        Return x.dtAssignmentDate < y.dtAssignmentDate 
    End Function
End Class
This would sort with a similar call, only you specify as parameter an instance of this sorter class: Array.Sort(tInput, New sorter)

Also different UI controls have different sorting mechanisms that could be utilized.
 
Back
Top