Re: Sorting a System.Collections.ObjectModel.Collection

Tech-Archive recommends: Speed Up your PC by fixing your registry



Arthur,
In addition to the other comments:


Remember that Collection(Of T) is simply a wrapper for an object that
implements IList(Of T). This wrapped IList(Of T) does all the "heavy
lifting" the Collection(Of T) simply delegates to the wrapped IList(Of T).
<sidenote>Read Collection(Of T) uses the Strategy Pattern. The IList(Of T)
is the strategy it uses to implement the list itself, by default its uses an
array like structure for the strategy (the List(Of T), you can easily
replace the strategy with a LinkedList or a Binary tree, see below for
details on replacing the strategy...</sidenote>

You can use Collection(Of T).Items to get at the wrapped IList(Of T).

By default Collection(Of T) uses a List(Of T) as the wrapped IList(Of T),
List(Of T) has a Sort method (plus a plethora of other methods).
Collection(Of T).Items is how you get at the wrapped IList(Of T). In other
words casting Collection(Of T).Items to List(Of T) should allow you to call
its Sort method.

Something like:

Public Class Person
Implements IComparable(Of Person)

...

Public Function CompareTo(ByVal other As Person) As Integer
Implements System.IComparable(Of Person).CompareTo
...
End Function

End Class

Public Class PersonCollection
Inherits ObjectModel.Collection(Of Person)

Public Sub Sort()
DirectCast(Items, List(Of Person)).Sort()
End Sub

End Class

I would consider introducing a new generic collection base that incorporates
this sorting:

Public Class CollectionBase(Of T)
Inherits ObjectModel.Collection(Of T)

Public Sub Sort()
DirectCast(Items, List(Of T)).Sort()
End Sub

Public Sub Sort(ByVal comparer As IComparer(Of T))
DirectCast(Items, List(Of T)).Sort(comparer)
End Sub

Public Sub Sort(ByVal index As Integer, ByVal count As Integer,
ByVal comparer As IComparer(Of T))
DirectCast(Items, List(Of T)).Sort(index, count, comparer)
End Sub

Public Sub Sort(ByVal comparison As Comparison(Of T))
DirectCast(Items, List(Of T)).Sort(comparison)
End Sub

End Class

Then PersonCollection would be based on this new base:

Public Class PersonCollection
Inherits CollectionBase(Of Person)

End Class


NOTE: The above may fail if you call Collection(Of T).New(IList(Of T))
passing in a something other then List(Of T). For example:

Public Class PersonCollection
Inherits ObjectModel.Collection(Of Person)

' use a linked list strategy
Public Sub New()
MyBase.New(New LinkedList(Of Person))
End Sub

' use an array strategy
Public Sub New()
MyBase.New(New Person(100-1) {}) ' base it on an 100 element
array
End Sub

' use some other generic collection strategy
' where SpecializedCollection implements IList(Of T)
Public Sub New()
MyBase.New(New SpecializedCollection(Of Person))
End Sub

End Class

The Frameworks LinkedList(Of T) does not implement IList(Of T) although it
is easy to add. The array does work (as the wrapped list), giving you a
fixed sized collection that you cannot add or remove elements from (you can
only change existing entries)...

I have not yet posted my sample LinkedList(Of T) that implements IList(Of T)
to my web site...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Arthur Dent" <hitchhikersguideto-news@xxxxxxxxx> wrote in message
news:eAOTJuVTGHA.1728@xxxxxxxxxxxxxxxxxxxxxxx
| How do you sort a generic collection derived from
| System.Collections.ObjectModel.Collection?
|
| Thanks in advance,
| - Arthur Dent
|
|


.



Relevant Pages

  • Re: Sorting a System.Collections.ObjectModel.Collection
    ... Remember that Collectionis simply a wrapper for an object that ... Listhas a Sort method. ... Public Class PersonCollection ... Public Sub Sort() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Windows Forms - Datagrid sort by hidden column
    ... DataView Wrapper - SDataView.vb ... Private _iBindingList As IBindingList ... Public Sub AddMapping(ByVal Column As String, ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Overcoming a MsgBoxs shyness
    ... I don't know, however, if you need to use MessageBox, make a wrapper for it ... Public Sub ShowMsgBox(Prompt As String, ... Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, ...
    (microsoft.public.vb.general.discussion)
  • Re: Problem creating TemplateFields in DetailsView dynamically
    ... if we only create the new TemplateField ... Public Sub New ... Dim l As New Literal ... Public Class MyStringEditItemTemplate ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • Re: Shared Properties disappearing
    ... Public Class AttributeCollection ... Protected intCollection As New Collection ... Public Sub Remove ... Public Function GetEnumerator() As System.Collections.IEnumerator ...
    (microsoft.public.dotnet.framework.aspnet)