key value pair with a list

S

Someone

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.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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.
 
S

Someone

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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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.
 
S

Someone

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.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top