Re: Sorted Dictionary / List woes.
Ryan Heath <[email protected]> Tue, 13 Nov 2007 16:57:12 +0100
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
In that case you could keep 2 collections in sync yourself. A dictionary<long,MessageInfo> for ID lookup and a List<MessageInfo> for the order of the messages. At insertions you should use List.BinarySearch, which can take an IComparer, to determine at which index the (new) MessageInfo belongs. That way the list is always sorted. // Ryan On Nov 13, 2007 4:42 PM, Bhatnagar, Amit <[email protected]> wrote: > well that's part of the problem. See I may receive a bunch of messsages > at any time, but they won't necessarily be in chronological order. There > is no way for me to insert them into the list in order. > > > > -----Original Message----- > From: Discussion forum for developers using Windows Forms to build apps > and controls [mailto:[email protected]] On Behalf Of > Ryan Heath > Sent: Tuesday, November 13, 2007 8:40 AM > To: [email protected] > Subject: Re: [DOTNET-WINFORMS] Sorted Dictionary / List woes. > > I believe a SortedList is what you want, but you should add the Messages > in order, to keep them *sorted* on DateTime. > > // Ryan > > On Nov 13, 2007 4:16 PM, Bhatnagar, Amit <[email protected]> > wrote: > > Hi All. > > > > I have a problem that I think a > > System.Collections.Generic.SortedList/Dictionary can solve, but I am > > unable to get it to work. > > > > I want to store a list of messages in a collection where I can check > > if an entry already exists quickly (dictionary), but also be able to > > return these messages in chronological order - so I thought a > > SortedDictionary is what I wanted to do here. > > > > So I create it like SortedDictionary<long, MessageInfo> where > > MessageInfo is a class that contains all the required info of a > > message, and the long key is a unique identifier so I know if I am > > adding duplicates. > > > > Now what I wanted to do is create a IComparer<> on the DateTime within > > > the MessageInfo so that the SortedDictionary would use that to sort, > > but it seems that the IComparer I make has to work on the type of the > > key (in this case, long), or it won't complile. I guess it is only > > ment to sort the keys of the key/value pair. > > > > Does anyone know what else I could do so I can have a collection that > > is keyed on a unique ID, but also have the ability to return these > > values in chronological order? > > > > thanks. > > > > > > The information contained in this e-mail message is PRIVATE. It may > contain confidential information and may be legally privileged. It is > intended for the exclusive use of the addressee(s). If you are not the > intended recipient, you are hereby notified that any dissemination, > distribution or reproduction of this communication is strictly > prohibited. If the intended recipient(s) cannot be reached or if a > transmission problem has occurred, please notify the sender immediately > by return e-mail and destroy all copies of this message. > > Thank you. > > >