473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How do I sort a KeyedCollection?

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 6861
Any idea's how I can sort a KeyedCollection. Thank you.
May 31 '07 #2
SammyB
807 Expert 512MB
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 SortedDictionary?
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 Expert 4TB
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 Expert 512MB
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 Expert 4TB
Ahh I see it's part of the VisualBasic compatibility. (And also in System.Collections.ObjectModel.KeyedCollection)
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
Thank you for your replies.

Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collections.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 Expert 4TB
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.Collections.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
Thank you very much. I will try it out.
Jun 1 '07 #9
SammyB
807 Expert 512MB
Thank you for your replies.

Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collections.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 SortedDictionary as your base class.
Jun 2 '07 #10

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

Similar topics

7
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...
0
by: James T. | last post by:
Hello! How I can update an object in KeyedCollection. whileItem propery is Read Only? Thank you! James
0
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
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...
4
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...
3
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...
10
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))...
8
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...
2
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 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.