473,387 Members | 1,904 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,387 software developers and data experts.

Sorted List in C#

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
6 13049
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
"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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniele Varrazzo | last post by:
If you need a container to look into, there is the sets module that provides a couple of them. If you need a sorted list, there is the bisect module. But i don't think it fits your need for a...
57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
5
by: MackS | last post by:
Dear all, I've got several large sets in my program. After performing several operations on these I wish to present one set to the user sorted according to a certain criterion. Is there any...
10
by: Der Andere | last post by:
I need to implement a sorted (ordered) list. STL has no sorted list type as far as I know. Is there a (straight) way to implement a sorted list using STL? BTW: The type of items in the list will...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
1
by: J L | last post by:
I want to create a sorted list whose values are themselves sorted lists. I wrote the following simple test program but it does not behave as I would expect. What I wanted to do was have the...
4
by: shrishjain | last post by:
Hi All, I need a type where I can store my items in sorted order. And I want to keep adding items to it, and want it to remain sorted. Is there any type in .net which I can make use of. I see...
1
by: Khayrat | last post by:
Hi folks, please help I have a xml file with a list of items. The list is sorted during xslt processing. Based on this sorted list I want a navigation facility to walk through the sorted list...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.