Grasshopper

algorithmic modeling for Rhino

Sorting using Keys & Values (aka Synchronous Sorting)

Sorting data is a common task in programming. Unfortunately only numbers and text have a well-defined sorting behaviour, and even text can be pretty ambiguous depending on the language it is in and the local culture setting of the system.

Other types of data -say colours- do not have an obviously correct way to sort them. Should colours be sorted by brightness, or amount of red, or hue, or saturation? All of these could be correct depending on the problem at hand and therefore none of them can be designated as the sensible default. The same is true for 3-dimensional entities such as points or meshes. When you sort a collection of meshes there's an almost infinite amount of metrics you could apply; volume, area, vertex-count, elevation, distance to some arbitrary point, ratio of width vs. height etc. etc. etc.

When you need to sort data using some arbitrary rule, you have to use the synchronous sorting feature in Grasshopper (this type of sorting is usually called 'Key-Value' sorting in programming). The basic idea is that you sort your unsortable data using a collection of sortable proxies. Let's say you want to sort a collection of curves from shortest to longest. The first thing you need to do is compute the length of each curve, so that you have a collection of numbers (which are sortable). Then you can sort the lengths while synchronously sorting the curves. The change in order that is applied to the collection of numbers is also applied to the collection of curves, thus putting them in a shortest-longest sorting order.

You can zoom in on the Sorting component to add any number of additional synchronous inputs/outputs, all of them will be re-ordered in the same way as the K input. 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Views: 39531

Replies to This Discussion

Thank you for the explanation David.
Can I dare to ask one more thing:

Basically, the way this Sort component works, is that it "remembers" (bad word maybe) the indexes of the list that goes into K input of the Sort component, and A input of the sort component.

If K input are always numbers, that means that in A, I can input, what ever I want - circles, polylines, curves, numbers, letters, text, whatever.
The way Sort component will work, is that, it will sort the K output numbers by decreasing or increasing, and it will "remember" the indexes of each of these sorted numbers. Then it will use this indexes, to sort the A input.

The same goes for: B, C, D, E ... outputs

Am I wrong?

And K input always needs to be a list of numbers?

You're basically correct. The .NET framework array base type allows for key-value sorting of two arrays simultaneously. I do not know how it is implemented internally.

Yes, the A, B, C etc. fields can be any data. That is of course the whole point of key-value sorting. And yes, at the moment only numbers are allowed. The Sets.Strings panel also has a Sort Strings component which allows for a single value collection to be added. I'll probably make it so that in future releases you can add more inputs for synchronous sorting.

Note that sorting strings is not as crisply defined as sorting numbers. There are all kinds of cultural dimensions to it that might affect the sort order of a specific collection of strings.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thank you for the reply David.

I did not just understand you about one thing: "Note that sorting strings is not as crisply defined as sorting numbers."

Is this what you meant:

If I understood it correctly, I need to input the data in form of strings separately in order to create list out of it.

Just untag "multyline string" tag inside panel editor (at the bottom)

Nope, I means that different languages (called 'cultures' or 'locales' in programming) treat text differently. See this post: http://msdn.microsoft.com/en-us/library/a7zyyk0c.aspx

--

David Rutten

david@mcneelcom

Poprad, Slovakia

There is a separate Sort Strings component on the Sets Tab > String Panel.

Danny what is the difference between these components? Sort List and Sort Strings?

Every string can be converted to list, by simply disabling the "Multiline Data" option for the panel which has string in it. Or with solutions you presented here.

In that case, only difference I noticed between the Sort List and Sort Strings is the that Sort Strings can also sort letters (example B), while Sort List can only sort numbers (example A).

Btw, the beginning panels on the left in all four definitions are strings, I just disabled the "Multiline Data" in all of them:

Is this the only difference?
Thank you.

Again, I am not trying to act like smart, contradict or confront your opinion. I am a newbie in the field of Grasshopper, and I am just trying to ask, whatever I am confused with.
English is my second language, so if sometimes my questions sound like "hey, you are not right, I know better than you", then that is probably a result of my lame knowledge of English, surely not intention.


@David Rutten : Thank you.
@Philipp : It worked. 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