What is difference between Function and Key


#1

Hi all. I’m new to Dynamo.

Im in trobule…while studying Dynamo code called “SortByFunction, SortByKey”…

I have no Idea… What is Function… and What is Key…

Please Help me…


#2

If you use List.SortByKey you give an order of indices and the list will sort on that basis. It’s very straightforward.

I don’t know how ListSortByFunction works. But considering how everything in Dynamo works with functions I can tell you that there will be a huge amount of possibilities.


#3

See if this helps explains the difference …


#4

They sort of work the same. Sort by keys require that you place nodes to create a key for every item on a list.

Take a list like this: {"Dan - 4", "Tom - 1", "Kevin - 3", "Jacob - 2", } where you want to use the number at the end of each string to sort the names. To use the String.Substring node to get the last character in the string (-1 and 1 inputs). This will produce {4,1,3,2} as an output (stored in RAM). This new list can serve as the “key” to sort the original list by in a List.SortByKey node. The first output of this will be the sorted list, and the second output is the sorted keys.

The same can be achieved with a List.SortByFunction. By not wiring the original list into the String.Substring node, it will stay as a function that gets the last character from a string. This can be the Function input in a List.SortByFunction node, and use the original list as the list input. The output from that will be the sorted list.

SortByFunction is sometimes a bit faster as there is less data stored in the graph - the sort functions are performed as a calculation inside the node, and all intermediate steps to obtain the sort order, and the end values are discarded - the sole output is the sorted list. It can be difficult to compose comparable functions though (an art in and of itself), and as there is only the one output, if you have to gather that data a second time you have to run the functions again as well.

My personal preference is List.SortByKey, and generally this should be enough to ‘get by’ in most cases, though the List.SortByFunction shouldn’t be forgotten as it is very useful as well.


#5

@JacobSmall @Vikram thank you Jacob and Vikram!!

Your comments were really helpful!

What I understand is that both of them sort their lists of data in ascending order based on the key or function. Is that true???


#6

Correct. The important part is knowing when to use which. That’s the sort of thing which comes in time as you use the program more and more. :slight_smile:


#7

@JacobSmall I really appreciate you Jacob!!