Sorting list based on several variables


#1

I want to sort a list based on several variables, and they all have their own importance.
In this case I have 3 levels of “importance” (all are just lists of values).


The top list is the list (of lists) that needs to be sorted.
Beneath that are the lists of values that it needs to be sorted on, the “importance” of them from top to bottom. Currently, the top one has all the same values (that won’t be the case in all situations) so the order can stay the same. In the second list the lowest value is at first position so that needs to move towards the top.
For the third list that would mean that that one would change in order to:
650
1250
1100
650
and the last 3 would need to get sorted based only on the last 3 values. as the first value is already “split” from them because of the sorting from the second list.

I hope I am making sense.

Thanks in advance!


Sort list 1 (wich contains a substring of list 2) according to the order of list 2
#2

It would be great, if you could post a picture (or just text) with the unsorted list and the sorted list. So I can see the difference between those two lists. Would be much simpler to understand.


#3

I could, but the list isn’t actually sorted based on anything within the list.


#4

Let me give you an example instead (the original list doesn’t really matter as in the end I’m more than anything after a list of indices).


I hope this somewhat clears things up.


#5

With this little python snippet your list gets sorted like in your table.

from operator import itemgetter
# List gets sorted by index 1, then 2, then 3
OUT = sorted(IN[0], key=itemgetter(1, 2, 3))

Grouping the list
#6

This might work amazingly but I don’t have a clue how to attach it to how Dynamo currently works.


#7

An example of using “itemgetter”:
image

The package “operator” should be usable in the IronPython environment.
The following is from the documentation of IronPython in regards to sorting:


#8

For your information: I don’t know anything about Python, let alone IronPython.


#9

Create a Python Node, copy my snippet into the Node and as input you have to connect your list to it. You don’t need to change something in the python script. Just copy and paste.


#10

this is great, much quicker than using GroupByKey or SortByKey, thanks :slight_smile:


List sort by key by z then by y then by x location
#11

I tried that but considering I have 3 lists that control the order and the thing I’m trying to sort is already a list of lists I don’t know how to actually attach it to make it work.


#12

Hi @PauLtus I tried replicating your original list and use the proposed from @jbo


#13

But If you look back up original comment you can see there’s actually 3 lists of lists between that initial list that are supposed to make up the order, not that first list.


#14

Well guys.
After finding a dumb oversight it works as it should. Thank you all very much!