Sort a collection class?
-
Does anyone have any idea how I can go about sorting a collection class. The elements of the class are: Public PointData As Double Public RateData As Double Public TimeData As Double The requirement is to sort on one of the following: PointData (asc) + RateData (Desc) or PointData (Desc) Any help would be very much appreciated!! :zzz: The code of the class collection is: Public Class CPRTData Public PointData As Double Public RateData As Double Public TimeData As Double ' Private data for the write-once ID property. Private mstrID As String ' The first time the ID property is set, the static ' Boolean is also set. Subsequent calls return !ERROR! in the ID. Public Property ID() As String Get ID = mstrID End Get Set(ByVal Value As String) Static blnAlreadySet As Boolean If Not blnAlreadySet Then blnAlreadySet = True mstrID = Value Else mstrID = "!ERROR!" End If End Set End Property End Class Public Class CPRTDataItems Implements System.Collections.IEnumerable 'local variable to hold collection Private mCol As Collection Public Function Add() As String 'create a new object Dim objNewMember As CPRTData Dim sKey As String = "" sKey = "R" & Format(mCol.Count + 1, "0000") objNewMember = New CPRTData objNewMember.ID = sKey 'set the properties passed into the method mCol.Add(objNewMember, sKey) 'return the object created Add = objNewMember.ID objNewMember = Nothing End Function Public Function GetEnumerator() As System.Collections.IEnumerator _ Implements System.Collections.IEnumerable.GetEnumerator GetEnumerator = mCol.GetEnumerator End Function Public Sub Clear() Dim iLoopVar As Integer For iLoopVar = mCol.Count To 1 Step -1 Remove(iLoopVar) Next End Sub Public Function Count() As Integer Count = mCol.Count() End Function Default Public ReadOnly Property Item(ByVal Index As Object) As CPRTData Get Item = mCol.Item(Index) End Get End Property Public Sub Remove(ByRef vntIndexKey As Object) 'used when removing an element from the collection 'vntIndexKey contains either the Index or Key, which is
-
Does anyone have any idea how I can go about sorting a collection class. The elements of the class are: Public PointData As Double Public RateData As Double Public TimeData As Double The requirement is to sort on one of the following: PointData (asc) + RateData (Desc) or PointData (Desc) Any help would be very much appreciated!! :zzz: The code of the class collection is: Public Class CPRTData Public PointData As Double Public RateData As Double Public TimeData As Double ' Private data for the write-once ID property. Private mstrID As String ' The first time the ID property is set, the static ' Boolean is also set. Subsequent calls return !ERROR! in the ID. Public Property ID() As String Get ID = mstrID End Get Set(ByVal Value As String) Static blnAlreadySet As Boolean If Not blnAlreadySet Then blnAlreadySet = True mstrID = Value Else mstrID = "!ERROR!" End If End Set End Property End Class Public Class CPRTDataItems Implements System.Collections.IEnumerable 'local variable to hold collection Private mCol As Collection Public Function Add() As String 'create a new object Dim objNewMember As CPRTData Dim sKey As String = "" sKey = "R" & Format(mCol.Count + 1, "0000") objNewMember = New CPRTData objNewMember.ID = sKey 'set the properties passed into the method mCol.Add(objNewMember, sKey) 'return the object created Add = objNewMember.ID objNewMember = Nothing End Function Public Function GetEnumerator() As System.Collections.IEnumerator _ Implements System.Collections.IEnumerable.GetEnumerator GetEnumerator = mCol.GetEnumerator End Function Public Sub Clear() Dim iLoopVar As Integer For iLoopVar = mCol.Count To 1 Step -1 Remove(iLoopVar) Next End Sub Public Function Count() As Integer Count = mCol.Count() End Function Default Public ReadOnly Property Item(ByVal Index As Object) As CPRTData Get Item = mCol.Item(Index) End Get End Property Public Sub Remove(ByRef vntIndexKey As Object) 'used when removing an element from the collection 'vntIndexKey contains either the Index or Key, which is
You should use a comparer to do sorting of collections. I have an article where I am using generic collections and doing sorting. I use comparers there. Here is the article link: http://www.codeproject.com/useritems/GridViewObjectDataSource.asp[^] Hope that helps. Ben
-
You should use a comparer to do sorting of collections. I have an article where I am using generic collections and doing sorting. I use comparers there. Here is the article link: http://www.codeproject.com/useritems/GridViewObjectDataSource.asp[^] Hope that helps. Ben