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

is there a type available for sorted list of items

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 there is SortedList<key, value> for hash tables, but could find
anything for a sorted list.

Currently I am using List<string> and whenever I add an item, I need to
call Sort() and it takes a long time - Order(nlogn). If the list is
already sorted, it will take just Order - logn. Does someone know if
there is a type where I can so such stuff.

Thanks,
Shrish

Apr 3 '06 #1
4 1837
If access by an index is what you're looking for, with SortedList<key,
value> you can also access its elements by an index using the Values or Keys
properties.

--
Nos vemos.
Guillermo
--------------
Microsoft VB MVP desde 1997
Mentor Asociado de Solid Quality Learning Iberoamericana

Te recuerdo que puedes entrar en mi sitio desde:
http://www.elguille.info/ y http://www.mundoprogramacion.com/
Los foros en: http://foros.elguille.info/
Si buscas un buen plan de alojamiento:
http://www.elguille.info/hostings/of...ing_guille.htm

<sh********@gmail.com> escribió en el mensaje
news:11**********************@j33g2000cwa.googlegr oups.com...
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 there is SortedList<key, value> for hash tables, but could find
anything for a sorted list.

Currently I am using List<string> and whenever I add an item, I need to
call Sort() and it takes a long time - Order(nlogn). If the list is
already sorted, it will take just Order - logn. Does someone know if
there is a type where I can so such stuff.

Thanks,
Shrish

Apr 4 '06 #2
Well, SortedList _is_ a list, but you have to tell it how to sort...
that's what the key is for. If you never look things up by the key,
just maintain the sorted order, then you don't lose anything.

Another possibility is to write a "lazy sort" class: one that allows
you to add items to the list without sorting, but sorts whenever you
try to iterate over the list, for example. All you need to do is keep a
"dirty" flag. Whenever you add an item to the list, flag the list as
"dirty". Whenever you do some operation that would require the list to
be sorted, if it's "dirty" then sort it before returning the result,
otherwise just return the result.

Apr 4 '06 #3
sh********@gmail.com wrote:
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 there is SortedList<key, value> for hash tables, but could find
anything for a sorted list.

Currently I am using List<string> and whenever I add an item, I need to
call Sort() and it takes a long time - Order(nlogn). If the list is
already sorted, it will take just Order - logn. Does someone know if
there is a type where I can so such stuff.


Note that the List<T> class has a BinarySearch method that will return
a positive or zero index for the item if it already exists in the list
or the one's complement of the position where the item should be, if
it's not in the list yet.

The list must be sorted for the BinarySearch method to work, but it's
quite fast to locate the item's position.

Therefore, instead of simply adding to the list, which would ruin the
sort order and require you to perform a explicit sort afterwards, you
could add the item at its sorted position by using BinarySearch to find
it for you (warning: untested VB code ahead!):

Dim Index As Integer = List.BinarySearch(Item)
If Index < 0 then List.InsertAt(Index Xor -1, Item)

And your list would be kept sorted...

Regards,

Branco.

Apr 4 '06 #4
That is very cool. I didn't know about that.

Here is the relevant documentation page:

http://msdn2.microsoft.com/en-us/lib...sh(VS.80).aspx

Apr 4 '06 #5

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

Similar topics

2
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
4
by: Cybertof | last post by:
Hi ! I would like to make an array of structure (collection ?) that would behave like an advanced sorted list : a sorted list with one key but with multiple values (sorted lists are key/value...
4
by: Olaf Baeyens | last post by:
I have to load about 10000 possible file names into a list and must have thems sorted alphabetically, including the file information. At this moment, in conventional C++, I use a sorted file strng...
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...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
13
by: vd12005 | last post by:
Hello, i would like to sort(ed) and reverse(d) the result of many huge dictionaries (a single dictionary will contain ~ 150000 entries). Keys are words, values are count (integer). i'm...
18
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
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;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.