Sorted List in C#

Z

Zoro

I come from the Delphi world and there we had a very useful component
called TStringList which was a dynamic string array which had a
'sorted' property and 'duplicate' property and it was great for
maintaining ordered lists allowing or disallowing duplicates.
Is there something similar in C#?
What I need in particular is an integer list that will keep itself
sorted automatically when i add items and that will ignore attempts to
add a duplicate values.
Is there such a list in C#?
Thanks for your help.
 
G

Guest

List is a lisf of elements, you can't guarantee element unique.
For this reason you need to use Set. BCL doesn't supply SortedSet
collections, you need to do some work manually.
See one of the samples over there http://www.codeproject.com/csharp/sets.asp

Zoro said:
I come from the Delphi world and there we had a very useful component
called TStringList which was a dynamic string array which had a
'sorted' property and 'duplicate' property and it was great for
maintaining ordered lists allowing or disallowing duplicates.
Is there something similar in C#?
What I need in particular is an integer list that will keep itself
sorted automatically when i add items and that will ignore attempts to
add a duplicate values.
Is there such a list in C#?

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Mark Rae

Is there such a list in C#?

The Framework contains a SortedList object in the System.Collections
namespace which is, therefore, available to any .NET language, not just
C#...
 
B

Barry Kelly

Zoro said:
I come from the Delphi world and there we had a very useful component
called TStringList which was a dynamic string array which had a
'sorted' property and 'duplicate' property and it was great for
maintaining ordered lists allowing or disallowing duplicates.
Is there something similar in C#?
What I need in particular is an integer list that will keep itself
sorted automatically when i add items and that will ignore attempts to
add a duplicate values.
Is there such a list in C#?
Thanks for your help.

There is no exact analog. TStringList is also a list of objects as well
as strings, so one could view this as quite similar:

System.Collections.Generic.SortedList<TKey,TValue>

where a TStringList is a SortedList<string,object>.

It wouldn't be a big task to create your own class that used List<T> in
the background and used insertion and binary search
(List<T>.BinarySearch) to maintain the exact semantics of the Delphi
class.

-- Barry
 
C

Chris Saunders

Not exactly what you are looking for but
System.Collections.Generic.SortedList<TKey, TValue>
could be easily adapted to your requirements. This requires Visual Studio
2005.

If you don't have 2005 you could adapt System.Collections.SortedList.
I'm not an expert and someone else may have a better suggestion.

Regards
Chris Saunders
 
Z

Zoro

Thank you all. Looks like System.Collections.Generic.SortedList is the
most suitable tool for my needs. However, a brief look at books online
didn't show how you specify the treatment of duplicates.
Can you specify ignoring duplicates (i.e. don't add but don't throw an
exception either)?
 
M

Mark Rae

Thank you all. Looks like System.Collections.Generic.SortedList is the
most suitable tool for my needs. However, a brief look at books online
didn't show how you specify the treatment of duplicates.
Can you specify ignoring duplicates (i.e. don't add but don't throw an
exception either)?

See the MSDN docs:
http://msdn2.microsoft.com/en-us/library/ms132319(VS.80).aspx

The SortedList object is specifically designed to throw an exception when a
duplicate key is entered, so suports the ContainsKey method to check for
this first.
 

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

Top