Connecting Tech Pros Worldwide Forums | Help | Site Map

"Sorting" a hashtable

JezB
Guest
 
Posts: n/a
#1: Nov 16 '05
I have a hashtable (h) of class objects keyed by a string (k) - the class
(c) object stored in each hastable entry has two attributes (a1 and a2).

I want to iterate through the entries of the hashtable by ordering by one of
the attributes of the class (a1 or a2).

How can this easily be achieved ? I think I have to copy to an ArrayList
then write an IComparer - but I'm not sure if this is the best way.

Any advice appreciated.




John Wood
Guest
 
Posts: n/a
#2: Nov 16 '05

re: "Sorting" a hashtable


You can instantiate the ArrayList from the Keys property of the hashtable.
Then sort the ArrayList (no need to write a comparer). You can then iterate
through the keys in the arraylist and lookup the associated value in the
hashtable.

If you don't want that final step (coz of performance), you could write a
comparer for the object (assuming the key is also stored in the object),
instantiate the ArrayList from the Values property, and then sort using your
custom comparer. The ArrayList will then contain a sorted list of your
objects.

"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:eDxyCR5VEHA.3016@tk2msftngp13.phx.gbl...[color=blue]
> I have a hashtable (h) of class objects keyed by a string (k) - the class
> (c) object stored in each hastable entry has two attributes (a1 and a2).
>
> I want to iterate through the entries of the hashtable by ordering by one[/color]
of[color=blue]
> the attributes of the class (a1 or a2).
>
> How can this easily be achieved ? I think I have to copy to an ArrayList
> then write an IComparer - but I'm not sure if this is the best way.
>
> Any advice appreciated.
>
>
>[/color]


JezB
Guest
 
Posts: n/a
#3: Nov 16 '05

re: "Sorting" a hashtable


Won't that just order by the hashtable key ? I want to order by an attribute
in the hashtable values.

"John Wood" <spam@isannoying.com> wrote in message
news:eP$Ihb5VEHA.2184@TK2MSFTNGP10.phx.gbl...[color=blue]
> You can instantiate the ArrayList from the Keys property of the hashtable.
> Then sort the ArrayList (no need to write a comparer). You can then[/color]
iterate[color=blue]
> through the keys in the arraylist and lookup the associated value in the
> hashtable.
>
> If you don't want that final step (coz of performance), you could write a
> comparer for the object (assuming the key is also stored in the object),
> instantiate the ArrayList from the Values property, and then sort using[/color]
your[color=blue]
> custom comparer. The ArrayList will then contain a sorted list of your
> objects.
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:eDxyCR5VEHA.3016@tk2msftngp13.phx.gbl...[color=green]
> > I have a hashtable (h) of class objects keyed by a string (k) - the[/color][/color]
class[color=blue][color=green]
> > (c) object stored in each hastable entry has two attributes (a1 and a2).
> >
> > I want to iterate through the entries of the hashtable by ordering by[/color][/color]
one[color=blue]
> of[color=green]
> > the attributes of the class (a1 or a2).
> >
> > How can this easily be achieved ? I think I have to copy to an ArrayList
> > then write an IComparer - but I'm not sure if this is the best way.
> >
> > Any advice appreciated.
> >
> >
> >[/color]
>
>[/color]


John Wood
Guest
 
Posts: n/a
#4: Nov 16 '05

re: "Sorting" a hashtable


Ah, now that I've read your question properly...... ;)
Yeah sorry about that. So the attribute has some kind of parameter that
would become the sort key?
I think you will need to write a custom comparer, as the only way to get
hold of the attributes is through the type.

"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:O5gb125VEHA.2972@TK2MSFTNGP11.phx.gbl...[color=blue]
> Won't that just order by the hashtable key ? I want to order by an[/color]
attribute[color=blue]
> in the hashtable values.
>
> "John Wood" <spam@isannoying.com> wrote in message
> news:eP$Ihb5VEHA.2184@TK2MSFTNGP10.phx.gbl...[color=green]
> > You can instantiate the ArrayList from the Keys property of the[/color][/color]
hashtable.[color=blue][color=green]
> > Then sort the ArrayList (no need to write a comparer). You can then[/color]
> iterate[color=green]
> > through the keys in the arraylist and lookup the associated value in the
> > hashtable.
> >
> > If you don't want that final step (coz of performance), you could write[/color][/color]
a[color=blue][color=green]
> > comparer for the object (assuming the key is also stored in the object),
> > instantiate the ArrayList from the Values property, and then sort using[/color]
> your[color=green]
> > custom comparer. The ArrayList will then contain a sorted list of your
> > objects.
> >
> > "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> > news:eDxyCR5VEHA.3016@tk2msftngp13.phx.gbl...[color=darkred]
> > > I have a hashtable (h) of class objects keyed by a string (k) - the[/color][/color]
> class[color=green][color=darkred]
> > > (c) object stored in each hastable entry has two attributes (a1 and[/color][/color][/color]
a2).[color=blue][color=green][color=darkred]
> > >
> > > I want to iterate through the entries of the hashtable by ordering by[/color][/color]
> one[color=green]
> > of[color=darkred]
> > > the attributes of the class (a1 or a2).
> > >
> > > How can this easily be achieved ? I think I have to copy to an[/color][/color][/color]
ArrayList[color=blue][color=green][color=darkred]
> > > then write an IComparer - but I'm not sure if this is the best way.
> > >
> > > Any advice appreciated.
> > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


JezB
Guest
 
Posts: n/a
#5: Nov 16 '05

re: "Sorting" a hashtable


That's what I ended up having to do.

"John Wood" <spam@isannoying.com> wrote in message
news:%23YDe495VEHA.2844@TK2MSFTNGP09.phx.gbl...[color=blue]
> Ah, now that I've read your question properly...... ;)
> Yeah sorry about that. So the attribute has some kind of parameter that
> would become the sort key?
> I think you will need to write a custom comparer, as the only way to get
> hold of the attributes is through the type.
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:O5gb125VEHA.2972@TK2MSFTNGP11.phx.gbl...[color=green]
> > Won't that just order by the hashtable key ? I want to order by an[/color]
> attribute[color=green]
> > in the hashtable values.
> >
> > "John Wood" <spam@isannoying.com> wrote in message
> > news:eP$Ihb5VEHA.2184@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > You can instantiate the ArrayList from the Keys property of the[/color][/color]
> hashtable.[color=green][color=darkred]
> > > Then sort the ArrayList (no need to write a comparer). You can then[/color]
> > iterate[color=darkred]
> > > through the keys in the arraylist and lookup the associated value in[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > hashtable.
> > >
> > > If you don't want that final step (coz of performance), you could[/color][/color][/color]
write[color=blue]
> a[color=green][color=darkred]
> > > comparer for the object (assuming the key is also stored in the[/color][/color][/color]
object),[color=blue][color=green][color=darkred]
> > > instantiate the ArrayList from the Values property, and then sort[/color][/color][/color]
using[color=blue][color=green]
> > your[color=darkred]
> > > custom comparer. The ArrayList will then contain a sorted list of your
> > > objects.
> > >
> > > "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> > > news:eDxyCR5VEHA.3016@tk2msftngp13.phx.gbl...
> > > > I have a hashtable (h) of class objects keyed by a string (k) - the[/color]
> > class[color=darkred]
> > > > (c) object stored in each hastable entry has two attributes (a1 and[/color][/color]
> a2).[color=green][color=darkred]
> > > >
> > > > I want to iterate through the entries of the hashtable by ordering[/color][/color][/color]
by[color=blue][color=green]
> > one[color=darkred]
> > > of
> > > > the attributes of the class (a1 or a2).
> > > >
> > > > How can this easily be achieved ? I think I have to copy to an[/color][/color]
> ArrayList[color=green][color=darkred]
> > > > then write an IComparer - but I'm not sure if this is the best way.
> > > >
> > > > Any advice appreciated.
> > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread