Can I sort a KeyedCollection? If so how do I do it. Please, any help on this issue is very much appreciated. Thank you.
9 6861
Any idea's how I can sort a KeyedCollection. Thank you.
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? -
Dim s As New SortedDictionary(Of String, String)
-
s.Add("txt", "notepad.exe")
-
s.Add("bmp", "paint.exe")
-
s.Add("dib", "paint.exe")
-
s.Add("rtf", "wordpad.exe")
-
Notice that the key is first, not second as in collections.
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.
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: -
Dim c As New Collection
-
c.Add("notepad.exe", "txt")
-
c.Add("paint.exe", "bmp")
-
c.Add("paint.exe", "dib")
-
c.Add("wordpad.exe", "rtf")
-
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
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.
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.
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: -
public class mylist : System.Collections.ObjectModel.KeyedCollection<string,int>
-
{
-
protected override string GetKeyForItem(int item)
-
{
-
int idx = base.IndexOf(item);
-
foreach (string n in base.Dictionary.Keys)
-
{
-
if (base[n] == item)
-
{
-
return n;//or something
-
}
-
}
-
throw new Exception("The method or operation is not finished.");
-
}
-
public ICollection<string> Keys
-
{
-
get
-
{
-
return base.Dictionary.Keys;
-
}
-
}
-
}
-
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.
Thank you very much. I will try it out.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: James T. |
last post by:
Hello!
How I can update an object in KeyedCollection. whileItem propery is Read
Only?
Thank you!
James
|
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.
|
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...
|
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...
|
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...
|
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))...
|
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...
|
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
...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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...
| |