473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

flexibel sorting collection

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
Nov 21 '05 #1
3 1397
On Thu, 28 Oct 2004 13:22:22 +0200, marc wrote:
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.
Nov 21 '05 #2
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:88****************************@40tude.net...
On Thu, 28 Oct 2004 13:22:22 +0200, marc wrote:
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.

Nov 21 '05 #3
On Thu, 28 Oct 2004 16:28:29 +0200, John Smith wrote:
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:88****************************@40tude.net...
On Thu, 28 Oct 2004 13:22:22 +0200, marc wrote:
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.
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
3
by: Ben Fidge | last post by:
Hi What are the options for sorting a StringCollection? I'm quite surprised that StringCollection doesn't have any sorting capability built in! Thanks Ben
7
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
1
by: ŘuGvEd | last post by:
Here is the error: direct link to erro image : http://img473.imageshack.us/img473/1666/untitled2pp.jpg Hi, I have a gridview control on the page which is bound to business object via...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
11
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.