473,549 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

key value pair with a list

I wish to use a sorted list to store lists of strings. I am unsure as to
how things work. I want to do something like:

SortedList list = new SortedList();

for(...loop for string...) {
if( ! list.contains(k ey)) {
list.add("keyva l", new ArrayList(strin g));
else
list.GetValue() .add(string);
}
}

Can someone clarify if this concept is right. Is it the case that you can
add many elements with the same key value to the sortedlist and then
iterate through them, negating the need for an ArrayList to be the value.

Thanks for any help.
Jan 5 '06 #1
5 17631
Hi Someone,

SortedList doesn't allow duplication of the keys. The keys is used for 2
things: for access and for sorting. The list is sorted based on the *key*
not on the *value* if you want sorted list of strings where the strings can
be duplicated use simple ArrayList and call its Sort method to sort the
strings.
--

Stoitcho Goutsev (100)
"Someone" <no****@nospam. com> wrote in message
news:dp******** **@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...
I wish to use a sorted list to store lists of strings. I am unsure as to
how things work. I want to do something like:

SortedList list = new SortedList();

for(...loop for string...) {
if( ! list.contains(k ey)) {
list.add("keyva l", new ArrayList(strin g));
else
list.GetValue() .add(string);
}
}

Can someone clarify if this concept is right. Is it the case that you can
add many elements with the same key value to the sortedlist and then
iterate through them, negating the need for an ArrayList to be the value.

Thanks for any help.

Jan 5 '06 #2
On Thu, 5 Jan 2006 10:18:44 -0500, Stoitcho Goutsev (100) [C# MVP] wrote:
Hi Someone,

SortedList doesn't allow duplication of the keys. The keys is used for 2
things: for access and for sorting. The list is sorted based on the *key*
not on the *value* if you want sorted list of strings where the strings can
be duplicated use simple ArrayList and call its Sort method to sort the
strings.


Thanks, but I do not want a sorted list of strings. I want lists of string
groups (not necessarily similar), accessible by a key. If I just used one
dimensional array list the strings would not be grouped except by
alphabetic order. I could use an arraylist of arraylists for the string
groups, but this is awkward and I would have to create a custom class
complete with indexers etc. Please note I do not know up front what the
key values will be, as this will be generated from the strings as they are
read - once a key is generated the string can be allocated to it's group.

I thought a SortedList would be more suitable, but perhaps this can be
explained a little more.

Regards,
Someone
Jan 5 '06 #3
Someone,

What do you want to sort - the strings in the groups or the keys under which
these groups are added. I guest the former. SortedList sorts the *keys* not
the strings.

I believe if you want to use sorted list you should create the object using
the cosntructor that accepts custom IComparer. You can write this IComparer
in a way that it compares the actual string groups rather than their keys.
--

Stoitcho Goutsev (100)

"Someone" <no****@nospam. com> wrote in message
news:dp******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...
On Thu, 5 Jan 2006 10:18:44 -0500, Stoitcho Goutsev (100) [C# MVP] wrote:
Hi Someone,

SortedList doesn't allow duplication of the keys. The keys is used for 2
things: for access and for sorting. The list is sorted based on the *key*
not on the *value* if you want sorted list of strings where the strings
can
be duplicated use simple ArrayList and call its Sort method to sort the
strings.


Thanks, but I do not want a sorted list of strings. I want lists of
string
groups (not necessarily similar), accessible by a key. If I just used one
dimensional array list the strings would not be grouped except by
alphabetic order. I could use an arraylist of arraylists for the string
groups, but this is awkward and I would have to create a custom class
complete with indexers etc. Please note I do not know up front what the
key values will be, as this will be generated from the strings as they are
read - once a key is generated the string can be allocated to it's group.

I thought a SortedList would be more suitable, but perhaps this can be
explained a little more.

Regards,
Someone

Jan 5 '06 #4
On Thu, 5 Jan 2006 12:04:47 -0500, Stoitcho Goutsev (100) [C# MVP] wrote:
Someone,

What do you want to sort - the strings in the groups or the keys under which
these groups are added. I guest the former. SortedList sorts the *keys* not
the strings.


That's correct and is what I wanted. The question is more about what can
be associated with a key. I wanted an arraylist to be associated with a
key. the arraylists will contain strings. Can anybody provide a brief
example of how to do this?

Thanks.
Jan 5 '06 #5
Anything can be associated with a *key*. There is no special requirements
for the *value*. The *keys* on the other hand have implement IComparable if
custom comparator is not provided

ArrayList list = new ArrayList();
list.AddRange(n ew string[]{"One", "Two", "Three"});
sortedList.Add( "key1", list);

string str = ((ArrayList)sor tedList["key1"])[0];
--

Stoitcho Goutsev (100)

"Someone" <no****@nospam. com> wrote in message
news:dp******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
On Thu, 5 Jan 2006 12:04:47 -0500, Stoitcho Goutsev (100) [C# MVP] wrote:
Someone,

What do you want to sort - the strings in the groups or the keys under
which
these groups are added. I guest the former. SortedList sorts the *keys*
not
the strings.


That's correct and is what I wanted. The question is more about what can
be associated with a key. I wanted an arraylist to be associated with a
key. the arraylists will contain strings. Can anybody provide a brief
example of how to do this?

Thanks.

Jan 5 '06 #6

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

Similar topics

2
2081
by: Astra | last post by:
Hi everybody Need your help. I have a DB-extracted list of say 5 items per page, which have links on each one that takes you to more detailed info on the 'clicked' particular item. When you are taken to the more detail page I have a link to take you back to the list. This all works fine and is standard stuff. My problem is that the...
5
3216
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: someArray = 6; I know I can't get the same syntactic sugar as Perl offers, with the usage of a string as the array key surrounded by square brackets. ...
3
11859
by: suresh | last post by:
Hi all, I have a hash table and I want to modify the value of a key. Please let me know how can that be acheived. Thanks Suresh
2
3127
by: PABLO | last post by:
Hello I have a Hastable, which I need to order it by value and not by key. Regards
5
2757
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef const KeyT first_type;
4
1688
by: Christian Christmann | last post by:
Hi, here is my small program: Header file: Class myClass { public: void function();
6
5237
by: Bart Simpson | last post by:
I am wriing a messaging system and I need to mantain a list of subscribers(object) to topics (string). which of the ff representations is better (and why?) typedef pair<string, list<Subscriber> > topicSubscribers ; typedef list<topicSubscribers> subscriberList ; OR
2
2166
by: Sehboo | last post by:
I am trying to use key value pair list, but I don't want to use sorted list because it messes up my order. I am not sure what other options I have. Can anybody point? Thanks
2
1537
by: james | last post by:
(Sorry if this is a double post) Hi Guys, I am creating a delegate a couple times and passing in a local variable. However, when the delegate is invoked it uses the variable passed into the last created instance (sorry that was a mouthful). Here is an example from my code: private void dlgFavorites_Load(object sender, EventArgs e)
20
3046
by: Pat | last post by:
I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best code I could come up with for adding a new key, value to a dict mytable.setdefault( k, ).append( v ) In Perl, the code looks like this:
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.