471,350 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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(key)) {
list.add("keyval", new ArrayList(string));
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 17518
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(key)) {
list.add("keyval", new ArrayList(string));
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(new string[]{"One", "Two", "Three"});
sortedList.Add("key1", list);

string str = ((ArrayList)sortedList["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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Robert Oschler | last post: by
3 posts views Thread by suresh | last post: by
2 posts views Thread by PABLO | last post: by
5 posts views Thread by homsan toft | last post: by
4 posts views Thread by Christian Christmann | last post: by
6 posts views Thread by Bart Simpson | last post: by
2 posts views Thread by Sehboo | last post: by
2 posts views Thread by james | last post: by
20 posts views Thread by Pat | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.