By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,106 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,106 IT Pros & Developers. It's quick & easy.

Generic SortedList and SortedDictionary for comlpex sorting Q..

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.