not sure if there is an easy way to do this!

P

Paul

thanks for the additional information, will give it a try. I had to add the
public keyword to the class interface values to gain access when copying data
from the hash table out to an array list.
Public class foo
{
public String name;
--
Paul G
Software engineer.


Peter Duniho said:
Hi thanks that worked!, just wondering if you know if there is an easy
way to
sort the hash table, say based on name alphabetical? I can look through
the
hash table methods as well to see if there is anything.

The Hashtable itself can't be sorted. But you can certainly copy the data
from the Hashtable and sort that.

You could copy the keys to an Array or List<T> and sort it using a custom
comparer (one that only looks at the field you want sorted). For example
(using Jon's sample):

Hashtable grouped = ...;

/* initialize hash table... */

/* then... */
ICollection keys = grouped.Keys;
Foo[] keyArray = new Foo[keys.Count];

keys.CopyTo(keyArray, 0);
Array.Sort(keys, delegate(Foo foo1, Foo foo2) { return
foo1.name.CompareTo(foo2.name); });

Then the "keys" array will be all of your data items, sorted by name.

You would probably do well to just browse through the System.Collections
and System.Collections.Generic namespaces. There are a number of
different kinds of collections, each of which providing different
functionality appropriate to different needs.

Pete
 
P

Paul

Hi Pete was just trying to impliment the sort and everything worked up to
the actuall sort. Below I have listed what code I have. the Foo is abcdata.
I am not sure what to use for foo1 and 2 below with what you provided.

Array.Sort(keys, delegate(Foo foo1, Foo foo2) { return
foo1.name.CompareTo(foo2.name); });
as I am not sure what foo1 and foo2 is?

I have the following that works.
Arraylist abcdataout // is the output array list
Hashtable grouped = new Hashtable();
abcdata abcdataall
grouped [abcdataall]=abcdataall;
foreach (abcdata abcdataall in grouped.keys)
{
abcdataout.Add(abcdataall.name);
abcdataout.Add(abcdataall.number);
abcdataout.Add(abcdataall.user);
abcdataout.Add(abcdataall.startdate);
abcdataout.Add(abcdataall.enddate);
}

Icollection keys = grouped.Keys;
abcdata[] keyArray = new abcdata[keys.count];
keys.CopyTo(keyArray,0);
need to sort here?
I would loke to sort by name and then start date.
I have not used delegates before so a bit confused on the sort command, thanks








--
Paul G
Software engineer.


Peter Duniho said:
Hi thanks that worked!, just wondering if you know if there is an easy
way to
sort the hash table, say based on name alphabetical? I can look through
the
hash table methods as well to see if there is anything.

The Hashtable itself can't be sorted. But you can certainly copy the data
from the Hashtable and sort that.

You could copy the keys to an Array or List<T> and sort it using a custom
comparer (one that only looks at the field you want sorted). For example
(using Jon's sample):

Hashtable grouped = ...;

/* initialize hash table... */

/* then... */
ICollection keys = grouped.Keys;
Foo[] keyArray = new Foo[keys.Count];

keys.CopyTo(keyArray, 0);
Array.Sort(keys, delegate(Foo foo1, Foo foo2) { return
foo1.name.CompareTo(foo2.name); });

Then the "keys" array will be all of your data items, sorted by name.

You would probably do well to just browse through the System.Collections
and System.Collections.Generic namespaces. There are a number of
different kinds of collections, each of which providing different
functionality appropriate to different needs.

Pete
 
P

Peter Duniho

Hi Pete was just trying to impliment the sort and everything worked upto
the actuall sort. Below I have listed what code I have. the Foo is
abcdata.
I am not sure what to use for foo1 and 2 below with what you provided.

Well, in the code I posted, "foo1" and "foo2" are of type Foo, which is
the type you said you were using for your keys (I realize the name is
likely something other than Foo). You don't use anything for "foo1" and
"foo2"...those are the parameters for the delegate and the Sort method
passes in actual values.

So, where the code I posted will sort your keys according to the name
only, you could define a different anonymous method that sorts according
to name and start date:

delegate(Foo foo1, Foo foo2)
{
if (foo1.name == foo2.name)
{
return foo1.startdate.CompareTo(foo2.startdate);
}

return foo1.name.CompareTo(foo2.name);
}

Note that you of course still need to do this sort before you separate
your keys back out into this absurd "groups of five" array you've got.
You would use this delegate for sorting _instead_ of the one I posted
before.

That "groups of five" array is not useful for anything, because there's no
practical way to use the .NET functionality to do anything with it. Any
..NET operations you want to do, they need to happen on a normal array
where each element represents an entirely different instance of your data.

Pete
 
P

Paul

thanks for the reply, wondering if I should have used a structure for this?
anyhow I tried the following for the array.sort using a and b as instances
of the abcdata, just to see if I could get a simple sort to work but got some
runtime errors.

Array.Sort (keys, delegate(abcdata a, abcdata b)
{
return a.name.CompareTo(b.name);
})

errors
The best overloaded method match for
System.array.sort(System.array,system.array) has some invalid arguments

arg 1 cant convert from system.collections.icollection to system arra
cant convert from anonymous method to system array
 
P

Peter Duniho

thanks for the reply, wondering if I should have used a structure for
this?

A structure or class, yes. Either one would be much better than the
"groups of five" array you're apparently trying to use.
anyhow I tried the following for the array.sort using a and b as
instances
of the abcdata, just to see if I could get a simple sort to work but got
some
runtime errors.

Array.Sort (keys, delegate(abcdata a, abcdata b)
{
return a.name.CompareTo(b.name);
})

Well, what is the type of "keys"? Unless it's an "abcdata[]", that code
isn't going to work. The anonymous method parameters must have the same
type as the type of the array elements.

I don't really understand the confusion here. Assuming the code sample I
posted earlier is something you understand, all you have to do is change
the logic _within_ the anonymous method to match my follow-up. Presumably
you already figured out how to replace "Foo" in the example with something
meaningful. Just do the same thing here.

Pete
 
P

Paul

thanks for the additional information, I have something sorting now, ended up
using a structure and built a comparer class.
--
Paul G
Software engineer.


Peter Duniho said:
thanks for the reply, wondering if I should have used a structure for
this?

A structure or class, yes. Either one would be much better than the
"groups of five" array you're apparently trying to use.
anyhow I tried the following for the array.sort using a and b as
instances
of the abcdata, just to see if I could get a simple sort to work but got
some
runtime errors.

Array.Sort (keys, delegate(abcdata a, abcdata b)
{
return a.name.CompareTo(b.name);
})

Well, what is the type of "keys"? Unless it's an "abcdata[]", that code
isn't going to work. The anonymous method parameters must have the same
type as the type of the array elements.

I don't really understand the confusion here. Assuming the code sample I
posted earlier is something you understand, all you have to do is change
the logic _within_ the anonymous method to match my follow-up. Presumably
you already figured out how to replace "Foo" in the example with something
meaningful. Just do the same thing here.

Pete
 

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

Top