473,566 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic SortedList and SortedDictionar y 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 GetTotalAmountO fAllItems()....
}

I want to be able to sort the first SortedList based on the integer index , but also based on the OtherSortedList .GetTotalAmount OfAllItems() 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;

"OtherSortedLis t" m_List;

}

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

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 SortedDictionar y "

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

(the same in SortedList)

Any ideas ?

thanks

Jan 24 '07 #1
4 6755
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*****@commun ity.nospamwrote in message
news:%2******** ********@TK2MSF TNGP06.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 GetTotalAmountO fAllItems()....
}

I want to be able to sort the first SortedList based on the integer index ,
but also based on the OtherSortedList .GetTotalAmount OfAllItems() 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;
"OtherSortedLis t" m_List;
}
and than to be able to sort once based on the index integer , and once based
on the OtherSortedList .GetTotalAmount OfAllItems()
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
SortedDictionar y "
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
"OtherSortedLis t.GetTotalAmoun tOfAllItems()"
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.spam wrote in message
news:da******** **********@news read1.news.pas. earthlink.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*****@commun ity.nospamwrote in message
news:%2******** ********@TK2MSF TNGP06.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 GetTotalAmountO fAllItems()....
}

I want to be able to sort the first SortedList based on the integer index
, but also based on the OtherSortedList .GetTotalAmount OfAllItems() 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;
"OtherSortedLis t" m_List;
}
and than to be able to sort once based on the index integer , and once
based on the OtherSortedList .GetTotalAmount OfAllItems()
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
SortedDictionar y "
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.mic rosoft.comwrote in message
news:LZ******** ******@TK2MSFTN GHUB02.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
31650
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 through the Values collection but that seems a little long winded. I ended up using a sorted list because it has the IndexOfValue method. However I...
6
5928
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 replacing an STL map instance by a System.Collections.Generic.SortedDictionary instance. In need the collection to be ordered, and to have...
2
2044
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 and the value is a list of MTGTracer. The documentation states:
2
2018
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 I want to do is change a value. I can get the index using IndexOfKey. There is no "SetByIndex" in Generic. Note: the list has two variables...
1
3896
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: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; ...
2
1323
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 SortedList(Of String, StringWithInteger) To populate a generic CombBox StringWithInteger has two properties Str and Value
1
1629
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 elements from the list by index (I think). So: Dictionary<string, stringThisStringDictionary = new Dictionary<string, string>(); ---Code to populate...
1
3090
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 initialize a SortedList all at once from sorted data?
3
4466
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 and throw exceptions. I have since discovered that the accessors use the IComparer to find things and since it is set at -1 it gets a null return...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.