Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

sort using powershell assembly?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neil Chambers - 22 May 2007 16:35 GMT
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
Nicholas Paldino [.NET/C# MVP] - 22 May 2007 16:52 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi All,
>
[quoted text clipped - 19 lines]
> cheers,
> n
n3llyb0y - 22 May 2007 17:02 GMT
> 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
Nicholas Paldino [.NET/C# MVP] - 22 May 2007 17:27 GMT
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> > });

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>> Neil,
>>
[quoted text clipped - 12 lines]
> Cheers,
> n
n3llyb0y - 23 May 2007 06:22 GMT
> Neil,
>
[quoted text clipped - 21 lines]
> Array.Sort(fruitInfo, delegate(FruitInfo x, FruitInfo y) { <code to do
> comparison here> > });

Thanks Nicholas :-)
Neil Chambers - 06 Jun 2007 14:16 GMT
> Neil,
>
[quoted text clipped - 21 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.