Generic SortedList and SortedDictionary for comlpex sorting Q..

S

semedao

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
 
M

Michael D. Ober

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

semedao

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)
 
L

Linda Liu [MSFT]

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<T> to 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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

semedao

thanks
Linda Liu said:
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<T> to 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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

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