473,325 Members | 2,828 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,325 software developers and data experts.

Generic SortedList and SortedDictionary for comlpex sorting Q..

Hi,
I want to implement list of key-values that can be sort by 2 ways.
let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another SortedList like this:

class OtherSortedList : SortedList
{
.....
int GetTotalAmountOfAllItems()....
}

I want to be able to sort the first SortedList based on the integer index , but also based on the OtherSortedList.GetTotalAmountOfAllItems() value
if I will put the OtherSortedList as the TValue than how can I Sort it ?

So , i consider to make a new class that will be the TKey of the SortedList

class MyKey
{

int Index;

"OtherSortedList" m_List;

}

and than to be able to sort once based on the index integer , and once based on the OtherSortedList.GetTotalAmountOfAllItems()

using some IComparer implementation

the problem is that the OtherSortedList is changing all time , elements will be add and remove (but not the index field that stay the same after initialization)

and "Keys must be immutable as long as they are used as keys in the SortedDictionary "

http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx

(the same in SortedList)

Any ideas ?

thanks

Jan 24 '07 #1
4 6741
Take advantage of the fact that you can assign the same object to multiple
collections and lists with very low overhead. Create a class that contains
two sorted lists internally and implement the desired methods on this class.
Remember that the list changing methods must operate on both internal lists
atomically.

Mike Ober.
"semedao" <se*****@community.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Hi,
I want to implement list of key-values that can be sort by 2 ways.
let's say that in the first step I wanted to make SortList based on Key =
int index that cannot change and Value is another SortedList like this:

class OtherSortedList : SortedList
{
.....
int GetTotalAmountOfAllItems()....
}

I want to be able to sort the first SortedList based on the integer index ,
but also based on the OtherSortedList.GetTotalAmountOfAllItems() value
if I will put the OtherSortedList as the TValue than how can I Sort it ?

So , i consider to make a new class that will be the TKey of the SortedList

class MyKey
{
int Index;
"OtherSortedList" m_List;
}
and than to be able to sort once based on the index integer , and once based
on the OtherSortedList.GetTotalAmountOfAllItems()
using some IComparer implementation
the problem is that the OtherSortedList is changing all time , elements will
be add and remove (but not the index field that stay the same after
initialization)
and "Keys must be immutable as long as they are used as keys in the
SortedDictionary "
http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx
(the same in SortedList)
Any ideas ?
thanks

Jan 24 '07 #2
thanks , but how it will target my problem that I need sorted list that the
Key is changing after adding it to the list ??
in the example before I will sort the list using
"OtherSortedList.GetTotalAmountOfAllItems()"
when the items in the internal OtherSortedList add and remove AFTER I added
this instance as key.

What can happen to the sortedList/Dictionary if the key is changing ? (when
I can ensure that there will not be a duplicate keys after the change)

"Michael D. Ober" <obermd.@.alum.mit.edu.no.spamwrote in message
news:da******************@newsread1.news.pas.earth link.net...
Take advantage of the fact that you can assign the same object to multiple
collections and lists with very low overhead. Create a class that
contains two sorted lists internally and implement the desired methods on
this class. Remember that the list changing methods must operate on both
internal lists atomically.

Mike Ober.
"semedao" <se*****@community.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Hi,
I want to implement list of key-values that can be sort by 2 ways.
let's say that in the first step I wanted to make SortList based on Key =
int index that cannot change and Value is another SortedList like this:

class OtherSortedList : SortedList
{
....
int GetTotalAmountOfAllItems()....
}

I want to be able to sort the first SortedList based on the integer index
, but also based on the OtherSortedList.GetTotalAmountOfAllItems() value
if I will put the OtherSortedList as the TValue than how can I Sort it ?

So , i consider to make a new class that will be the TKey of the
SortedList

class MyKey
{
int Index;
"OtherSortedList" m_List;
}
and than to be able to sort once based on the index integer , and once
based on the OtherSortedList.GetTotalAmountOfAllItems()
using some IComparer implementation
the problem is that the OtherSortedList is changing all time , elements
will be add and remove (but not the index field that stay the same after
initialization)
and "Keys must be immutable as long as they are used as keys in the
SortedDictionary "
http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx
(the same in SortedList)
Any ideas ?
thanks


Jan 24 '07 #3
Hi Semedao,

When we use SortedList<TKey,TValue>, we can only sort the list based on the
key. To sort the sorted list by a value within the value objects, the only
way is to comprise the value object into the corresponding key. Thus the
comparer has a chance to sort based on the value within the value objects.

Your thinking of making a new class MyKey for the TKey of the SortedList
works in theory, because you are modifying the property, i.e. m_List within
the key object, not the key itself, e.g. setting the key to a new object,
when elements are added and removed to the sorted list.

However, I don't think it is good practice to do like that. The primary
purpose of the key in a collection of key-value pairs is to make it
convenient to access the value in the collection with a 'keyword'.
Generally speaking, the key is of simple type, e.g. string, int and etc. In
your workaround, the key loses the function of 'keyword' and only takes the
role for sorting.

I noticed that you have mentioned that you'd like the key in the SortedList
to be equal to the index of the corresponding key-value pair. It looks to
me that you don't really need the 'keyword' function of the key in the
SortedList. If so, why not use List<Tto store your OtherSortedList
objects, instead of SortedList<TKey,TValue>? You could implement IComparer
for the type OtherSortedList and then call the Sort method of the List<T>
to sort the list. Of course, if you'd like 2 ways to sort the list, you
should implement the IComparer in two ways.

Hope this helps.
If my suggestion is not appropriate to your practice, please feel free to
let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 24 '07 #4
thanks
"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:LZ**************@TK2MSFTNGHUB02.phx.gbl...
Hi Semedao,

When we use SortedList<TKey,TValue>, we can only sort the list based on
the
key. To sort the sorted list by a value within the value objects, the only
way is to comprise the value object into the corresponding key. Thus the
comparer has a chance to sort based on the value within the value objects.

Your thinking of making a new class MyKey for the TKey of the SortedList
works in theory, because you are modifying the property, i.e. m_List
within
the key object, not the key itself, e.g. setting the key to a new object,
when elements are added and removed to the sorted list.

However, I don't think it is good practice to do like that. The primary
purpose of the key in a collection of key-value pairs is to make it
convenient to access the value in the collection with a 'keyword'.
Generally speaking, the key is of simple type, e.g. string, int and etc.
In
your workaround, the key loses the function of 'keyword' and only takes
the
role for sorting.

I noticed that you have mentioned that you'd like the key in the
SortedList
to be equal to the index of the corresponding key-value pair. It looks to
me that you don't really need the 'keyword' function of the key in the
SortedList. If so, why not use List<Tto store your OtherSortedList
objects, instead of SortedList<TKey,TValue>? You could implement IComparer
for the type OtherSortedList and then call the Sort method of the List<T>
to sort the list. Of course, if you'd like 2 ways to sort the list, you
should implement the IComparer in two ways.

Hope this helps.
If my suggestion is not appropriate to your practice, please feel free to
let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jan 26 '07 #5

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

Similar topics

2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
6
by: Paul Delhanty | last post by:
Hi, I am converting an existing native C++ program making heavy use of STL to C#2.0 with STL usage replaced by the new generic collections. The conversion has gone well to the point where I am...
2
by: newscorrespondent | last post by:
I have a list declared: public System.Collections.Generic.SortedList<int, System.Collections.Generic.List<MTGTracer ActiveList = new SortedList<int,List<MTGTracer>>(); The key is an integer...
2
by: Bruce Arnold | last post by:
I have a program that works fine using Remove and Add to update a value. The program processes a log file from a router and counts the hits based on url. It bothers me to use Remove/Add when all...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
2
by: active | last post by:
This is where a different thread ended. The subject of this query is quite different from that of the other thread so I thought I should start a new thread. I'm using Private mItemList As...
1
by: Ethan Strauss | last post by:
Hi, I have been using a variety of generic collections and I really like them, but I have just noticed something weird ... If you have a generic Dictionary (or SortedList), you can retrieve...
1
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
3
dcharnigo
by: dcharnigo | last post by:
I thought I would be tricky and force the SortedList to not sort using a custom IComparer, I used the following code, but when the list is sorting using the custom sort the accessors become unusable...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.