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.