By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,973 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,973 IT Pros & Developers. It's quick & easy.

Sorted List in C#

P: n/a
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.

May 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" wrote:
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

May 11 '06 #2

P: n/a
"Zoro" <il****@gmail.com> wrote in message
news:11*********************@q12g2000cwa.googlegro ups.com...
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#...
May 11 '06 #3

P: n/a
"Zoro" <il****@gmail.com> wrote:
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
May 11 '06 #4

P: n/a
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

"Zoro" <il****@gmail.com> wrote in message
news:11*********************@q12g2000cwa.googlegro ups.com...
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.

May 11 '06 #5

P: n/a
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)?

May 11 '06 #6

P: n/a
"Zoro" <il****@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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/lib...19(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.
May 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.