473,230 Members | 1,704 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,230 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 17621
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
5
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: ...
3
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
by: PABLO | last post by:
Hello I have a Hastable, which I need to order it by value and not by key. Regards
5
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...
4
by: Christian Christmann | last post by:
Hi, here is my small program: Header file: Class myClass { public: void function();
6
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,...
2
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
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...
20
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.