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

Hi,

I’m trying to sort a list of families by location. First sort by z then group, then sort those groups by y then by x.

I’ve gotten as far as sorting and grouping by z, then sorting the groups by y. I’m not sure how to continue and sort them by x without losing the y sort I already did. One way would be grouping them again by Y, but I tried that and ended up in sub-list hell. Any suggestions?

Oh, and I also found a sort points by XY node (as seen in the screenshot), but I’m not sure how I can use it.

Have you tried searching the Forum?

This might be of help?

I did search the forum. My node setup was actually partially inspired by this post.

In your example they used a sort by function, which I am not able to use since I’m using separate components of location points (x,y, and z), and not the family instance location function itself.

My problem is that when I sort by y, then try to sort the result by x, the y becomes scrambled again.
Below is the continuation of the above screenshot.

maybe this will help…

You could try something like this:

import clr
orglst = IN[0]
orglst.sort(key = lambda orglst: (orglst.X, orglst.Y, orglst.Z))
OUT = orglst

@adam_bear1 and @Jonathan.Olesen, thanks for the pointers.

However, in my case, since my goal is not to sort the list itself, but sort another list by keys, I’m not sure how to use the code you suggested? I guess one way is to store the 3 lists of point components into variables and then use those variables as keys for the sort argument. I don’t know if that would work though, and I can’t try it out myself because I don’t know python.

I’ve attached the file and script so that someone more knowledgeable can easily try it out.
sort family instances by location.dyn (13.3 KB)
sort family instances by location.rvt (832 KB)

Not perfect but it works:

import clr
from operator import itemgetter
orglst = IN[0]
OUT = sorted(orglst, key=itemgetter(1,2,3))
2 Likes

Hmm, I tried your script, but it’s still not sorting the X at the end for some reason. I flipped the itemgetter(1,2,3) to itemgetter(3,2,1) since I want to sort by Z first and X last.

It sorted by Z then by Y but failed to sort by X, as you can see in the screenshot below. What do you think is the reason?

Instead of flipping the itemgetter rather flip your definition of “pt.X” and “pt.Z” :slight_smile:

2 Likes

Ah, that was perfect! Thanks a lot @Jonathan.Olesen

Could you please explain why it was necessary to include a Math.Round to 2 digits? I tried without it at first and it didn’t work.

The reason why the rounding “helps” is that your points without rounding is actually not all the same "location in the Y point, it is just an inaccuracy beyond 3 delimiters…

This is without rounding (sorted Z,Y,X) and as you can see it is in fact correct (as Y beats X). Though with 3 delimiters it looks wrong. The rounding combats that :slight_smile:

1 Like

I see now, that explains a lot, thanks. :slight_smile:

Hi!
Someone get this error?? =/

@joaoluiz.sribeiro Please start a new topic

Please read and copy carefully. It is spelled clr not crl…
Also basic troubleshooting skills. The error is on a specific line so double check that line before asking for help.

1 Like