467,880 Members | 1,253 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,880 developers. It's quick & easy.

sort using powershell assembly?

Hi All,

I'm looking to see if it's feasible to use the SortObjectCommand
included in the Microsoft.Powershell.Commands assembly.

I have a Dictionary

Dictionary<string, int[,]d = new Dictionary<string, int[,]>();

d["apples"] = new int[2,2];
d["peaches"] = new int[2,2];

Assuming we have some data in the value arrays, I would then like to
sort on the keys where d.value[1,1] is higher (for example).

There are of course a number of ways to code the sort, but I'm
interested in using the powershell assembly - I just can't fathom how
to use it in this context.

Any ideas?

cheers,
n

May 22 '07 #1
  • viewed: 2357
Share:
5 Replies
Neil,

Are you using this for another applet you are going to use in
powershell, or as part of your program? If you are going to use it as part
of your program, you are better off using the sorting functionality that is
provided to you by the framework.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Neil Chambers" <n3******@aol.comwrote in message
news:2007052216353875249-n3llyb0y@aolcom...
Hi All,

I'm looking to see if it's feasible to use the SortObjectCommand included
in the Microsoft.Powershell.Commands assembly.

I have a Dictionary

Dictionary<string, int[,]d = new Dictionary<string, int[,]>();

d["apples"] = new int[2,2];
d["peaches"] = new int[2,2];

Assuming we have some data in the value arrays, I would then like to sort
on the keys where d.value[1,1] is higher (for example).

There are of course a number of ways to code the sort, but I'm interested
in using the powershell assembly - I just can't fathom how to use it in
this context.

Any ideas?

cheers,
n

May 22 '07 #2
On 2007-05-22 16:53:21 +0100, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comsaid:
Neil,

Are you using this for another applet you are going to use in
powershell, or as part of your program? If you are going to use it as
part of your program, you are better off using the sorting
functionality that is provided to you by the framework.
It's for a program. I am trying to learn C#, in part by converting some
powershell scripts I have. I couldn't find any reference to sorting
within the framework so thought I'd have to write my own algorithm or
use the powershell assembly.

If you can point me to some documentation that would be great!

Cheers,
n

May 22 '07 #3
Neil,

In this case, I really wouldn't use a dictionary for storing the values.
Rather, I would have a structure like this (change the names appropriately):

public struct FruitInfo
{
public string Fruit;
public int[,] Values;
}

Then, you would have instances of your structures that you create in an
array.

Once you have all of these, you can call the static Sort method on the
Array class, passing a delegate to the comparison parameter to determine the
sort order:

// The array of FruitInfo instances.
FruitInfo[] fruitInfo = ...;

// Assume you have populated the array of fruitInfo instances.
Array.Sort(fruitInfo, delegate(FruitInfo x, FruitInfo y) { <code to do
comparison here });
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"n3llyb0y" <n3******@aol.comwrote in message
news:2007052217020350073-n3llyb0y@aolcom...
On 2007-05-22 16:53:21 +0100, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comsaid:
>Neil,

Are you using this for another applet you are going to use in
powershell, or as part of your program? If you are going to use it as
part of your program, you are better off using the sorting functionality
that is provided to you by the framework.

It's for a program. I am trying to learn C#, in part by converting some
powershell scripts I have. I couldn't find any reference to sorting within
the framework so thought I'd have to write my own algorithm or use the
powershell assembly.

If you can point me to some documentation that would be great!

Cheers,
n

May 22 '07 #4
On 2007-05-22 17:27:30 +0100, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comsaid:
Neil,

In this case, I really wouldn't use a dictionary for storing the
values. Rather, I would have a structure like this (change the names
appropriately):

public struct FruitInfo
{
public string Fruit;
public int[,] Values;
}

Then, you would have instances of your structures that you create
in an array.

Once you have all of these, you can call the static Sort method on
the Array class, passing a delegate to the comparison parameter to
determine the sort order:

// The array of FruitInfo instances.
FruitInfo[] fruitInfo = ...;

// Assume you have populated the array of fruitInfo instances.
Array.Sort(fruitInfo, delegate(FruitInfo x, FruitInfo y) { <code to do
comparison here });
Thanks Nicholas :-)

May 23 '07 #5
On 2007-05-22 17:27:30 +0100, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comsaid:
Neil,

In this case, I really wouldn't use a dictionary for storing the
values. Rather, I would have a structure like this (change the names
appropriately):

public struct FruitInfo
{
public string Fruit;
public int[,] Values;
}

Then, you would have instances of your structures that you create
in an array.

Once you have all of these, you can call the static Sort method on
the Array class, passing a delegate to the comparison parameter to
determine the sort order:

// The array of FruitInfo instances.
FruitInfo[] fruitInfo = ...;

// Assume you have populated the array of fruitInfo instances.
Array.Sort(fruitInfo, delegate(FruitInfo x, FruitInfo y) { <code to do
comparison here });
I'm getting close but I could do with a little more guidance.

Within the comparison code, will I be making use of 'Comparer<myStruct>'?

I can't seem to get a working statement. I need the Comparer to access
an index point within the 'Values' element of the object but I'm
failing. I'm clearly in at the deep end here but a little explanation
of how to get the sort working on the delegates would be much
appreciated.

Cheers,
n

Jun 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by =?Utf-8?B?VGhvbWFzIFcuIEJyb3du?= | last post: by
6 posts views Thread by Mesan | last post: by
3 posts views Thread by Marco Shaw | last post: by
1 post views Thread by Alexander Vasilevsky | last post: by
3 posts views Thread by sridhard2406 | last post: by
24 posts views Thread by =?Utf-8?B?QnJpYW4gTmljaG9sc29u?= | last post: by
1 post views Thread by =?Utf-8?B?bGVyb3ljYXJs?= | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.