Reorder list of list base by new order of index

Hi user, as I was working on some project, The node “D” “List.SortByKey” did not work as what I would like it to be. Basically what I want to achieve is that from Node “A” to be reorder base by the point that I have extracted in Node “C” which has a new order as compared to the old order Node “B”. I can’t seem to be able to reorder my Node “B” base on my node “C”. But as mention the objective is to reorder my Node “A” instead of reordering Node “B”.

In the pic I am reordering Node “B” to take a small step first instead of taking a big leap in reordering Node “A” and I really hope to learn this process step by step to really understand how sort by key works.

SortByKey will sort a list (let’s call it the input list) by another list of values (keys), regardless of the data in the first list. In your case, SortByKey understands nothing about the contents of the input list so it doesn’t make a comparison with the new list, it simply sorts the keys (e.g. Point(X = -0.358… will appear before Point(X = 3.242… and so on) and then reorders the input list in the same way (if the index of the sorted keys are now 0,2,1,4,3 then the input list will be reordered 0,2,1,4,3)

If I understand correctly, you are trying to order a 2D list by another 1D list which has the same subset of values (the points), but in a different order. One solution is to use dictionaries, if (and it’s a very important if) you don’t have duplicate points, because dictionaries have unique keys and if you have points with the same coordinates then it gets more complex. There’s a guide to dictionaries in Dynamo here: https://primer.dynamobim.org/09_Dictionaries/9-1_What-is-a-dictionary.html

I have generated some data that looks similar to yours in A.

  • You can reduce the number of nodes used at B, by using List@Level on ListGetItemAtIndex rather than List.Map (the node FirstItem would also work as well in this situation and reduce the number of nodes needed further)
  • Use String from Object to turn the point data into a string for onward use as the dictionary key (this could be streamlined by extracting the XYZ values and concatenating into a shorter key)
  • Assemble a dictionary using Dictionary.ByKeysValues with the string as the key, and the list from A as the values
  • C shuffles the points to simulate your list of points in a different order from B
  • Again, use String from Object to turn the point data into a string. These strings need to be in the same format as the dictionary keys
  • Finally, use Dictionary.ValueAtKey to query the dictionary to return a list of your data in the shuffled order

See the attached dyn. Hope this helps,
Thomas


dictionary.dyn (29.4 KB)

3 Likes

Thank you so much it did work well. didn’t know the process is a lot easier than I thought it will be. As I using dynamo 1.3.4 I should start using 2.0 version instead to look into more node that has been produce in this version. Thank you once again.

1 Like

You’re welcome. Yes, Dictionary nodes were introduced in Dynamo 2. With Dynamo 1.3 you could use an List.FirstIndexOf and List.SortByKey as an alternative