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

**URL:** <https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227>\
**Category:** Lists-Logic\
**Tags:** dynamo, list-filter\
**Created:** [June 29, 2018, 8:14am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227 "2018-06-29T08:14:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sed07](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sed07](https://forum.dynamobim.com/u/sed07)\
**Post date:** [June 29, 2018, 8:14am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/1 "2018-06-29T08:14:01Z")

</div>

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.

 ![sort%20by%20z%20and%20group%20then%20y%20then%20x](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/c/2c36188aebb50323450eac4a700aca0c22451969.PNG)

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [June 29, 2018, 8:22am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/2 "2018-06-29T08:22:51Z")

</div>

Have you tried searching the Forum?

This might be of help?

> [@Sort by multiple keys](https://forum.dynamobim.com/t/sort-by-multiple-keys/3145/3):
>
> Thanks Vikram, I want something that goes similar to the example below, I almost got there, but I want it for 3 different parameters.

---

<div class="post-metadata">

**Author:** ![sed07](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sed07](https://forum.dynamobim.com/u/sed07)\
**Post date:** [June 29, 2018, 8:37am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/3 "2018-06-29T08:37:42Z")

</div>

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

> [@Sort a sublist](https://forum.dynamobim.com/t/sort-a-sublist/4577/5):
>
> This should do the needful… [Hurðanummar-20160618-1.dyn](https://forum.dynamobim.com/uploads/short-url/cLYn0k1QKh2zpOxSNarIBCO5kZR.dyn) (12.9 KB)

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.

 ![sort%20by%20z%20and%20group%20then%20y%20then%20x2](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/1/21bc1918091181aeb434b75caa8c331e974543d9.PNG)

---

<div class="post-metadata">

**Author:** ![adam\_bear1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/adam_bear1/32/13411_2.png) [@adam\_bear1](https://forum.dynamobim.com/u/adam_bear1)\
**Post date:** [June 29, 2018, 8:46am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/4 "2018-06-29T08:46:50Z")

</div>

maybe this will help…

> [@Sorting list based on several variables](https://forum.dynamobim.com/t/sorting-list-based-on-several-variables/21005/10):
>
> this is great, much quicker than using GroupByKey or SortByKey, thanks slight_smile

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [June 29, 2018, 8:56am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/5 "2018-06-29T08:56:21Z")

</div>

You could try something like this:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/3/73c19d70de56e82f22aec01a1d70d47259fa9f9c.png)

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

---

<div class="post-metadata">

**Author:** ![sed07](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sed07](https://forum.dynamobim.com/u/sed07)\
**Post date:** [June 29, 2018, 9:54am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/6 "2018-06-29T09:54:25Z")

</div>

@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](https://forum.dynamobim.com/uploads/short-url/ojDzwk0uQAls2bEbr8MpvI33oik.dyn) (13.3 KB)  
[sort family instances by location.rvt](https://forum.dynamobim.com/uploads/short-url/qG1w6xHXAdxppmpp3zCpCjOJsFG.rvt) (832 KB)

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [June 29, 2018, 10:19am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/7 "2018-06-29T10:19:25Z")

</div>

Not perfect but it works:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/6/16003c00bd0e8128088fa8ca48557bbc0e6df3c3.png)

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

---

<div class="post-metadata">

**Author:** ![sed07](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sed07](https://forum.dynamobim.com/u/sed07)\
**Post date:** [June 29, 2018, 11:07am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/8 "2018-06-29T11:07:06Z")

</div>

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?

 ![sort%20by%20z%20and%20group%20then%20y%20then%20x4](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/2/e2591b5572502f208ae177d86121fb94b2a54241.PNG)

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [June 29, 2018, 11:10am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/9 "2018-06-29T11:10:06Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/f/aff2691e37b5473ad0841a2f4080c8f8188c10e0.png)

---

<div class="post-metadata">

**Author:** ![sed07](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sed07](https://forum.dynamobim.com/u/sed07)\
**Post date:** [June 29, 2018, 11:31am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/10 "2018-06-29T11:31:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [June 29, 2018, 11:37am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/11 "2018-06-29T11:37:41Z")

</div>

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 🙂

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/f/9f32d99e2d6b00b9bc7ca73b3f7a5b644202899b.png)

---

<div class="post-metadata">

**Author:** ![sed07](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sed07](https://forum.dynamobim.com/u/sed07)\
**Post date:** [June 29, 2018, 11:52am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/12 "2018-06-29T11:52:10Z")

</div>

I see now, that explains a lot, thanks. 🙂

---

<div class="post-metadata">

**Author:** ![joaoluiz.sribeiro](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@joaoluiz.sribeiro](https://forum.dynamobim.com/u/joaoluiz.sribeiro)\
**Post date:** [March 20, 2019, 4:18am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/13 "2019-03-20T04:18:07Z")

</div>

Hi!  
Someone get this error?? =/

 ![Sem%20t%C3%ADtulo](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/9/592c5c0b4f66e68386fc1c8997cae4db4872084a.png)

---

<div class="post-metadata">

**Author:** ![salvatoredragotta](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/salvatoredragotta/32/10078_2.png) [@salvatoredragotta](https://forum.dynamobim.com/u/salvatoredragotta)\
**Post date:** [March 20, 2019, 4:42am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/14 "2019-03-20T04:42:33Z")

</div>

@joaoluiz.sribeiro Please start a new topic

---

<div class="post-metadata">

**Author:** ![kennyb6](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kennyb6](https://forum.dynamobim.com/u/kennyb6)\
**Post date:** [March 20, 2019, 5:36am UTC](https://forum.dynamobim.com/t/list-sort-by-key-by-z-then-by-y-then-by-x-location/23227/15 "2019-03-20T05:36:47Z")

</div>

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.
