sort using powershell assembly?

  • Thread starter Thread starter Neil Chambers
  • Start date Start date
N

Neil Chambers

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
 
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.
 
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
 
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> > });
 
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 :-)
 
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
 
Back
Top