flexibel sorting collection

  • Thread starter Thread starter marc
  • Start date Start date
M

marc

Hi,

I have a collection of objects and i would like to add a flexibel sorting
functionality

I am looking for a colliection that gives my the following functionality
a - have enumaration to loop through all objects {for each}
b - check if item exists, without throwing exceptions!!! {if
col.exists(key)}
c - extract item from collection bases on key {x=col(key)}
d - I would like to sort on number of properties
{col.sort(porperty enum)}

a,b and c I can get by using sortedlist, but it has no flexible sorting

Any ideas??????

thankx
Marc
 
a,b and c I can get by using sortedlist, but it has no flexible sorting

The SortedList should meet your needs. The constructor for SortedList is
overloaded to allow you to pass in an IComparer instance. You can create
different classes that implement IComparer for each of your sorting styles
and pass in the appropriate one as needed.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Dear Chris,

This approach works if you know which Icomparer instance you want to use
when you construct the SortedList but what if you want to (re) sort an
existing list, say first by name, and later on by birth day?

Thanks,

Sven

Chris Dunaway said:
a,b and c I can get by using sortedlist, but it has no flexible sorting

The SortedList should meet your needs. The constructor for SortedList is
overloaded to allow you to pass in an IComparer instance. You can create
different classes that implement IComparer for each of your sorting styles
and pass in the appropriate one as needed.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Dear Chris,

This approach works if you know which Icomparer instance you want to use
when you construct the SortedList but what if you want to (re) sort an
existing list, say first by name, and later on by birth day?

Thanks,

Sven

Chris Dunaway said:
a,b and c I can get by using sortedlist, but it has no flexible sorting

The SortedList should meet your needs. The constructor for SortedList is
overloaded to allow you to pass in an IComparer instance. You can create
different classes that implement IComparer for each of your sorting styles
and pass in the appropriate one as needed.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Perhaps the class that implements IComparer could have a property
indicating the sort order or method. If you change the property, the sort
order changes. I haven't tested it but something like this:

Public Class MySorter
Implements IComparer

Private m_SortOrder As String

Public Property SortOrder() As String
Get
Return m_SortOrder
End Get
Set(ByVal Value As String)
m_SortOrder = Value
End Set
End Property

Public Function Compare(...) As Integer Implements IComparer.Compare
Select Case m_SortOrder
Case "Ascending"
'Code here
Case "Descending"
'Code Here
End Select
End Function
End Class

Dim MySorter As New MyClassThatImplementsIComparer
Dim MyList As New SortedList(MySorter)

' other code

'Need to change sort order:
MySorter.SortOrder = "Descending"


In other words, keep the instance of the sorter around so you can set its
property. The Compare routine in the class checks this property and
compares accordingly

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top