Re: Sorting a System.Collections.ObjectModel.Collection
- From: "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxxxxxxxx>
- Date: Fri, 24 Mar 2006 07:26:58 -0600
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
|
|
.
- Follow-Ups:
- Re: Sorting a System.Collections.ObjectModel.Collection
- From: Arthur Dent
- Re: Sorting a System.Collections.ObjectModel.Collection
- References:
- Sorting a System.Collections.ObjectModel.Collection
- From: Arthur Dent
- Sorting a System.Collections.ObjectModel.Collection
- Prev by Date: Re: .net input box
- Next by Date: Re: .net input box
- Previous by thread: Re: Sorting a System.Collections.ObjectModel.Collection
- Next by thread: Re: Sorting a System.Collections.ObjectModel.Collection
- Index(es):
Relevant Pages
|