473,675 Members | 3,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I sort a KeyedCollection ?

5 New Member
Can I sort a KeyedCollection ? If so how do I do it. Please, any help on this issue is very much appreciated. Thank you.
May 29 '07 #1
9 6905
bchirra
5 New Member
Any idea's how I can sort a KeyedCollection . Thank you.
May 31 '07 #2
SammyB
807 Recognized Expert Contributor
Any idea's how I can sort a KeyedCollection . Thank you.
I cannot see anyway to do this. In fact, I cannot see any way to retrieve the keys from a collection. Can you convert your Collection to a SortedDictionar y?
Expand|Select|Wrap|Line Numbers
  1. Dim s As New SortedDictionary(Of String, String)
  2. s.Add("txt", "notepad.exe")
  3. s.Add("bmp", "paint.exe")
  4. s.Add("dib", "paint.exe")
  5. s.Add("rtf", "wordpad.exe")
  6.  
Notice that the key is first, not second as in collections.
May 31 '07 #3
Plater
7,872 Recognized Expert Expert
I could not find any object called KeyedCollection .
The only collections with Keys I have seen have a .Keys proprty that returns a string[]. And that is very easy to sort.

Must sort functions (like the generic Array.Sort() ) take a custom thing to sort objects (Icomparable or something like that) in which you can define you own method to decide which comes first between two objects.
May 31 '07 #4
SammyB
807 Recognized Expert Contributor
I could not find any object called KeyedCollection .
You've made my day: I almost posted the same reply. I think that bchirra is using an old Collection object: remember, they could have keys:
Expand|Select|Wrap|Line Numbers
  1. Dim c As New Collection
  2. c.Add("notepad.exe", "txt")
  3. c.Add("paint.exe", "bmp")
  4. c.Add("paint.exe", "dib")
  5. c.Add("wordpad.exe", "rtf")
  6.  
As you said, if you could get to the keys, sorting would be easy, but these keys seem to be write-only! I had no idea that the Collection object was still around. --Sam
May 31 '07 #5
Plater
7,872 Recognized Expert Expert
Ahh I see it's part of the VisualBasic compatibility. (And also in System.Collecti ons.ObjectModel .KeyedCollectio n)
You could also have a second single-dimensioned array (ArrayList is good)
wherein whenever your Collection object is populated, you add the value of the KEY to that second array.
Then all you need to do is sort that array. Remember: Only the Keys need to be sorted if you reference your Collection object using the value of the Key.

I would def say time to upgrade to an actual collection object. Since KeyedColelction is abstract you must be creating a custom class that inherits KeyedCollection , at which point YOU are in charge of coding in your custum method to retrieve the key. So you are really digging your own hole on that one.
May 31 '07 #6
bchirra
5 New Member
Thank you for your replies.

Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collecti ons.ObjectModel (new class in .net 2.0).

Can I modify my class to inherit from generic SortedList? What is default sort order of the SortedList, Asce or Desc? Thank you.
Jun 1 '07 #7
Plater
7,872 Recognized Expert Expert
You could override the add function to track the key in your own way. (Still not sure why you are using this setup to begin with)

I did this, not sure it's correct:
Expand|Select|Wrap|Line Numbers
  1. public class mylist : System.Collections.ObjectModel.KeyedCollection<string,int>
  2.     {
  3.         protected override string GetKeyForItem(int item)
  4.         {
  5.             int idx = base.IndexOf(item);
  6.             foreach (string n in base.Dictionary.Keys)
  7.             {
  8.                 if (base[n] == item)
  9.                 {
  10.                     return n;//or something
  11.                 }
  12.             }
  13.             throw new Exception("The method or operation is not finished.");
  14.         }
  15.         public ICollection<string> Keys
  16.         {
  17.             get
  18.             {
  19.                 return base.Dictionary.Keys;
  20.             }
  21.         }
  22.     }
  23.  
Thank you for your replies.

Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collecti ons.ObjectModel (new class in .net 2.0).

Can I modify my class to inherit from generic SortedList? What is default sort order of the SortedList, Asce or Desc? Thank you.
Jun 1 '07 #8
bchirra
5 New Member
Thank you very much. I will try it out.
Jun 1 '07 #9
SammyB
807 Recognized Expert Contributor
Thank you for your replies.

Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collecti ons.ObjectModel (new class in .net 2.0).

Can I modify my class to inherit from generic SortedList? What is default sort order of the SortedList, Asce or Desc? Thank you.
If you have keys and you want to sort, then I would think that you would want to use SortedDictionar y as your base class.
Jun 2 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2447
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my sort()". I'd heard you can do this within a namespace: namespace Fixed { using namespace std;
0
911
by: James T. | last post by:
Hello! How I can update an object in KeyedCollection. whileItem propery is Read Only? Thank you! James
0
966
by: Craig Buchanan | last post by:
is there an easy way to serialize on the keys in a KeyedCollection, rather than the entire collection of objects? perhaps there is another option? thanks for your time.
3
1948
by: Brannon | last post by:
So when you use VS's View Designer, each object included is keyed off its Name. You cannot make the name the same as some other component. You can change the name in a PropertyGrid. I want to do a similar thing. I have a class (MissionManager with a bunch of static functions) that contains a static KeyedCollection<Missionof my objects. Each mission has a Name (and interface requring the name). That works fine. What doesn't work is changing...
4
4759
by: Dahab | last post by:
Hi, Anyone know if its possible to iterate through a hashtable in the same order that the table was populated? Im currently using an enumerator to iterate, but the order is quit different from when the items where inserted into the hashtable. Thanks D.
3
3993
by: Wiebe Tijsma | last post by:
Hi, I'm trying to serialize a KeyedCollection with the SoapFormatter, however I'm getting the exception: System.Runtime.Serialization.SerializationException: Soap Serializer does not support serializing Generic Types : System.Collections.Generic.GenericEqualityComparer`1 I didn't expose any generic types however, so it's internal to the Keyed
10
3777
by: Chris Mullins [MVP] | last post by:
KeyedCollection is a very handy little class, that unforutnatly has a nasty bug in it. The bug (which I ran across) causes the following code to fail: if (!rooms.Contains(room)) _rooms.Add(room); The problem is that contains returns "false", but then Add throws an exception because the item really is already in there.
8
3757
by: GS | last post by:
please bear with me, I already tried the built-in help, Google but not finding the solution. I want to use generic KeyedCollection first crack private class RegexHolder { String Name; bool Tested; RegexOptions regexOption;
2
4908
by: Jon Slaughter | last post by:
topic says it all? Is there any simple way? msdn says that the keyed collection is build from both a sortedlist and sorted dictionary. http://msdn2.microsoft.com/en-us/library/5z658b67.aspx (well, it doesn't say that specifically but it says it when talking about the sorted versions so I'm not sure) I've found this
0
8521
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8680
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8722
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7501
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6377
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2867
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
2
2125
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.