# Sorting list based on several variables

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.

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.

1 Like

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

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.

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))``````
5 Likes

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

An example of using “itemgetter”:

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

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

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.

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

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.

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

2 Likes

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.

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

2 Likes