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

Renumbering keys in a SortedList


Could somebody say why the piece of code below does not work? My purpose
is to renumber keys in a SortedList (after removal of an item) so that
the keys would always contain an unbroken sequence of integers starting
with 1. For some reason this is not the result.

IDictionaryEnumerator dictEnum = sortedList.GetEnumerator();

int i=1;
while ( dictEnum.MoveNext() ) {
DictionaryEntry de = (DictionaryEntry) dictEnum.Current;
de.Key = i++;
}

With best regards
Pekka
Nov 17 '05 #1
2 3058


Pekka wrote:
Could somebody say why the piece of code below does not work? My purpose
is to renumber keys in a SortedList (after removal of an item) so that
the keys would always contain an unbroken sequence of integers starting
with 1. For some reason this is not the result.
I don't think any of the .Net datastructures supports simulatneous
enumeration and editing.
IDictionaryEnumerator dictEnum = sortedList.GetEnumerator();
int i=1;
while ( dictEnum.MoveNext() ) {
DictionaryEntry de = (DictionaryEntry) dictEnum.Current;
de.Key = i++;
}


The above is equivalent to the rather prettier:

int i = 1;
foreach ( DictionaryEntry e in sortedList )
e.Key = i++;

I would think you would need to do:

IDictionary new_list = new SortedList(sortedList.Count);
int i = 1;
foreach ( DictionaryEntry e in sortedList )
new_list.Add(i++, e.Value);
sortedList = new_list

But seriously, the items are already sorted in sortedList, and there is
no way lookup the Key from a Value. The keys are obviously not related
to the values, so would you not much rather use a simple array of the
values?

object[] values = new object[sortedList.Count];
sortedList.Values.CopyTo(values, 0);

which you can lookup by integer keys *very* efficiently ;)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #2
> But seriously, the items are already sorted in sortedList, and there is
no way lookup the Key from a Value. The keys are obviously not related
to the values, so would you not much rather use a simple array of the
values?

object[] values = new object[sortedList.Count];
sortedList.Values.CopyTo(values, 0);

which you can lookup by integer keys *very* efficiently ;)


That's a nice idea :-) Thanks!

Pekka
Nov 17 '05 #3

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

Similar topics

3
by: amywolfie | last post by:
This is an interesting one..... I have a field on FORM!FORMDATA (no, I did not name this) called KEY. There can be multiple KEYS per record (equates to FIELDS on a given DOCUMENT). The...
2
by: buzz | last post by:
Is it possible to use multiple keys to sort a structure, such as LastName / FirstName in a sortedList? --------------= Posted using GrabIt =---------------- ------= Binary Usenet downloading...
2
by: Prez | last post by:
I started writing .net code yesterday and I am grasping it well enough. I have a few questions about SortedLists. I am using managed C++ if that makes any difference. Of the examples I...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
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...
5
by: ShaneO | last post by:
VB.NET 2005: I need to sort an array of Customer details based on transaction dates. If I use a SortedList the obvious problem is that it does not support duplicate keys. Does anyone have a...
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;...
7
by: pamela fluente | last post by:
I noticed that I am using quite often SortedLists where I use KEYS only. when I add new elements I use something like : MyKeyObj , Nothing/Null/false Is there a collection type which is...
3
dcharnigo
by: dcharnigo | last post by:
I thought I would be tricky and force the SortedList to not sort using a custom IComparer, I used the following code, but when the list is sorting using the custom sort the accessors become unusable...
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:
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?
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
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
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.