Match key, sort on value in key/value pair

  • Thread starter Thread starter Jim Adams
  • Start date Start date
J

Jim Adams

I'm counting the frequency of word occurances and would like to return
a key/value list sorted descending by frequency.

So I need to quickly see if a word (key) is in the list, but later
sort the list descending by values.

Any ideas on how to do this efficiently?

Thanks,

Jim
 
Jim,
Unfortunately the Framework has no predefined classes that do that, however
you can create a custom collection that encapsulates a Hashtable to allow
key/value, and an ArrayList that you can sort to return descending values.

There are any number of ways to implement this depending on what the
requirements of your program are.

1. Use a DictionaryBase with a method that populates & sorts the ArrayList
as needed.
2. Use a DictionaryBase with a private field for the ArrayList. The Add
method would add the object to both collections.
3. Use a CollectionBase with a private field for a HashTable. The Add method
would add the object to both collections.
4. Create a custom object that contains both a HashTable & ArrayList.

Note in #2, #3 & #4 above I would use an insertion sort to maintain a sorted
array as I added elements...

Hope this helps
Jay
 
Jim,

I think that the datatable with primary keys can exactly do what you want.

\\\just typed in, nothing checked
Dim dt As New DataTable
Dim keys(0) As DataColumn
dt.Columns.Add("Key")
dt.Columns.Add("Value")
keys(0) = dt.Columns(0)
dim dvValues as dataview(dt)
dv.sort = "Value"
etc
etc
///

You can use the dt.rows.find to find a key and the dv with the select for
the value.

I hope this helps?

Cor
 
Hi Jay,

Thanks for the tip.

-Jim

Jay B. Harlow said:
Jim,
Unfortunately the Framework has no predefined classes that do that, however
you can create a custom collection that encapsulates a Hashtable to allow
key/value, and an ArrayList that you can sort to return descending values.

There are any number of ways to implement this depending on what the
requirements of your program are.

1. Use a DictionaryBase with a method that populates & sorts the ArrayList
as needed.
2. Use a DictionaryBase with a private field for the ArrayList. The Add
method would add the object to both collections.
3. Use a CollectionBase with a private field for a HashTable. The Add method
would add the object to both collections.
4. Create a custom object that contains both a HashTable & ArrayList.

Note in #2, #3 & #4 above I would use an insertion sort to maintain a sorted
array as I added elements...

Hope this helps
Jay
 
Hi Cor,

Thanks for the tip.

Jim

Cor Ligthert said:
Jim,

I think that the datatable with primary keys can exactly do what you want.

\\\just typed in, nothing checked
Dim dt As New DataTable
Dim keys(0) As DataColumn
dt.Columns.Add("Key")
dt.Columns.Add("Value")
keys(0) = dt.Columns(0)
dim dvValues as dataview(dt)
dv.sort = "Value"
etc
etc
///

You can use the dt.rows.find to find a key and the dv with the select for
the value.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top