Grasshopper

algorithmic modeling for Rhino

Using List.Sort with delegates in C# script component ?

Hi!

I'm trying to sort a list of Polylines based on Z values within a C# script component using the List.Sort method and a delegate. However, after compiled, an error of 'not a delegate type' appears. Is there some way to create a delegate to do this ? Does it require the addition of another import such as system.linq ?

private void RunScript(List<Polyline> TopoCurves, object y, ref object A)
{

TopoCurves.Sort(delegate(Polyline a, Polyline b)
{
     return a.First.Z > b.First.Z;
});
}

Views: 1491

Replies to This Discussion

I'll prepare a "simple" example using a variety of sorting methods (and a challenge for the brave, he he).

In the mean time get the simplest possible way

Attachments:

This is great. Thank you!. I see you used the OrderBy method. What about the List.Sort method ? Is it possible to use that with a delegate ?

Here we are : 3 ways to skin a cat (and a BIG challenge: win 1m(*) sardines, no kidding. see inside).

PS: there's a dedicated category for code matters (freaks only please, he he)

(*) m for micron

best, Peter

Attachments:

And the final cut: 6 ways to skin a cat.

Spot the Generic one (only disabled minds please, he he).

PS: Added 2 more challenges for the (very) brave and/or for the (desperately) freak(s).

have fun, best, Peter

Attachments:

And the end of the affair:

1. Added a C# that makes topoCurves out of a given Brep. These are sorted "in Z order". The "resolution" is user controllable but Rhino is slow for doing that kind of stuff. Obviously we are talking about Curves and NOT polylines (see below).

2. Now ... if you toggle true the magic option "makeChaos" ... you'll get a chaotic collection of topo Curves as regards Z. Then you can feed this to the SortSomething C# (provided that you can address the challenge n3, he he) and have a measure of the performance of each Sorting method provided.

best, Peter

Attachments:

Er ... the code (function) used for "randomizing" could be the winner for the most stupid code ever - what was I "thinking"? (not much obviously, he he).

Get the corrected one (just one line and 123,45 times faster) plus a polyline output option.

Attachments:

Nice c# code, thank you!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service