# Dynamo List sorting question

**URL:** <https://forum.dynamobim.com/t/dynamo-list-sorting-question/24>\
**Category:** Packages\
**Created:** [January 6, 2014, 2:42pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24 "2014-01-06T14:42:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![seandburke](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seandburke/32/29153_2.png) [@seandburke](https://forum.dynamobim.com/u/seandburke)\
**Post date:** [January 6, 2014, 2:42pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/1 "2014-01-06T14:42:11Z")

</div>

I have two questions:

&nbsp;

How would I sort a list of XYZs by Z value, and then break into a list of lists sorted by X value?

&nbsp;

On a related note, why would the XYZ X node on the far right give varying values for the Z, especially when they were accurate, and verified by typing the height parameter value in Revit?

&nbsp;

![](http://dynamobim.com/wp-content/uploads/discussions/1-1000/51-Capture.png?width=750)

---

<div class="post-metadata">

**Author:** ![Andreas\_Dieckmann](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/andreas_dieckmann/32/365_2.png) [@Andreas\_Dieckmann](https://forum.dynamobim.com/u/Andreas_Dieckmann)\
**Post date:** [January 7, 2014, 10:56pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/2 "2014-01-07T22:56:41Z")

</div>

Hi guys,

  

I wanted to add another solution to the discussion as I have worked on a similar problem of sorting lists this week.  
I ended up using one of Python's abilities to handle lists which is called groupby() because I did not want my head to hurt thinking about how to go about this in Dynamo... ;-)

  

Here's how:

  

  
1. Sort list by X values using the method Steve explains above
  
2. Sort list by Z values   
(Notice that within each group of equal Z values, the X values retain their order from the previous sort operation)
  
3. Create a list of lists with XYZ values (sublist 0) and just Z values (sublist 1)
  
4. Group that list of lists by the Z values (using a custom node with some Python code inside)
  
5. Extract only the list of lists of XYZs by combining two map nodes
  

  

You will need the package **Group List of Lists By Key** for this to work.

  

(For what it's worth, I also uploaded another package called Sort List Of Lists this week which was related to that project but isn't needed here. But it might be worth checking out, too.)

  

here the other day. I am now using a custom node called Almost Zero (also in the package manager) as tolerance for XYZ-based calculations and rounding.

---

<div class="post-metadata">

**Author:** ![seandburke](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seandburke/32/29153_2.png) [@seandburke](https://forum.dynamobim.com/u/seandburke)\
**Post date:** [January 7, 2014, 4:18pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/3 "2014-01-07T16:18:18Z")

</div>

It is crazy. My head hurts, and I like it.

---

<div class="post-metadata">

**Author:** ![Julien\_BENOIT](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Julien\_BENOIT](https://forum.dynamobim.com/u/Julien_BENOIT)\
**Post date:** [January 7, 2014, 4:17pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/4 "2014-01-07T16:17:27Z")

</div>

Guys, thanks for the lesson. a perfect way to learn Map, Compose, etc…in fact how to use a node as a function. Excellent! crazy but excellent!

---

<div class="post-metadata">

**Author:** ![seandburke](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seandburke/32/29153_2.png) [@seandburke](https://forum.dynamobim.com/u/seandburke)\
**Post date:** [January 7, 2014, 12:32pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/5 "2014-01-07T12:32:50Z")

</div>

Curry, huh? How spicy? Hehe. I'll chew on this for a bit. It does appear to solve the problem. Thanks so much. I hope you still have most fo your hair, mine started falling out yesterday - and I suspect it's due to this challenge. ;)

---

<div class="post-metadata">

**Author:** ![Steve\_Elliott](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@Steve\_Elliott](https://forum.dynamobim.com/u/Steve_Elliott)\
**Post date:** [January 7, 2014, 11:51am UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/6 "2014-01-07T11:51:32Z")

</div>

Here's my (rather complicated) solution:

  

![](http://dynamobim.com/wp-content/uploads/discussions/1-1000/56-sorting.png?width=721)

  

This requires two packages from the Dynamo Package Manager: **remove duplicates** and **Curry**.

  

The idea is that you want a sub-list for each unique Z component of your XYZs. That means the goals are to find all unique Z values, then for each value filter the list so that only XYZs with that Z value remain.

  

  
1. Use **remove duplicates** to generate a list of unique Z components from the XYZs.
  
2. Build up a filter predicate by
  
  1. Create a function that accepts a number and returns true if it equals a certain unique Z component. Since **Map** expects a 1-argument function, and **Equal** is a 2-argument function, we need to use **Curry** to turn **Equal** into a 1-argument function that returns a function that will accept the second argument.
  
  2. Create a new function that first extracts the Z component from an XYZ and then passes it to the previous function.
  

  
  
3. Now you have a filter predicate for each unique Z component. You can **Map** over these, passing each into **Filter** , which will produce the sub-lists.
  
4. For each sub-list, sort by the X component using **Sort by Key**.
  

  
  

Obviously this workflow is a little crazy, so expect a node for this kind of thing in the future.

---

<div class="post-metadata">

**Author:** ![seandburke](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seandburke/32/29153_2.png) [@seandburke](https://forum.dynamobim.com/u/seandburke)\
**Post date:** [January 7, 2014, 9:45am UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/7 "2014-01-07T09:45:56Z")

</div>

Fantastic! What I’d then like to do is identify all Z values that are the same and break the lists… This will let me perform my end goal of numbering the elements (windows in this case) by column and by row. So, if there’s a series of Z values that are ‘3’ and another bunch ‘13’ etc, I can a break at the unique values.  
  
  
  
  
  
I really appreciate the help.

---

<div class="post-metadata">

**Author:** ![Steve\_Elliott](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@Steve\_Elliott](https://forum.dynamobim.com/u/Steve_Elliott)\
**Post date:** [January 7, 2014, 12:25am UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/8 "2014-01-07T00:25:59Z")

</div>

![](http://dynamobim.com/wp-content/uploads/discussions/1-1000/59-sortz.png?width=721)

In the workflow, I use the **XYZ Z** node as a function (note the missing input) that I pass to **Sort by Key** , which in turn applies it to every item in the list. The list is then sorted based on the output of **XYZ Z**.

  
  

Could you elaborate a little more on how you want to break this into sub-lists sorted by X axis?

  
  

The slight discrepancy between the numbers is due to floating-point inaccuracy in all computers, and can be ignored. The difference between the numbers is so minuscule that it doesn't matter.

---

<div class="post-metadata">

**Author:** ![Ian\_Siegel](https://avatars.discourse-cdn.com/v4/letter/i/bc79bd/32.png) [@Ian\_Siegel](https://forum.dynamobim.com/u/Ian_Siegel)\
**Post date:** [June 20, 2014, 5:11pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/9 "2014-06-20T17:11:27Z")

</div>

Interesting solutions here – thanks everyone for posting, it’s been a huge help.

Let’s say these XYZs are extracted from a list of Revit family instances. This method sorts their XYZ locations into groups, but how might I group the Revit instances into these groups (instead of only grouping their XYZs)?

---

<div class="post-metadata">

**Author:** ![Colin\_McCrone](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_McCrone](https://forum.dynamobim.com/u/Colin_McCrone)\
**Post date:** [June 23, 2014, 8:08pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/10 "2014-06-23T20:08:48Z")

</div>

I think you are looking for something like Andreas’s _Group list of lists by key_ package, or I also just submitted something similar to the package manager. Look for _Group Items By Key List_.

![Capture](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/1X/744d335efb9ff39c7dbef7338fefd344a1f372f3.png)

---

<div class="post-metadata">

**Author:** ![Andreas\_Dieckmann](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/andreas_dieckmann/32/365_2.png) [@Andreas\_Dieckmann](https://forum.dynamobim.com/u/Andreas_Dieckmann)\
**Post date:** [June 24, 2014, 12:30am UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/11 "2014-06-24T00:30:18Z")

</div>

Not sure if my package actually works in 0.7 - haven’t tested it yet. You better go with Colin’s package if you’re running 0.7 …

---

<div class="post-metadata">

**Author:** ![Colin\_McCrone](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_McCrone](https://forum.dynamobim.com/u/Colin_McCrone)\
**Post date:** [June 24, 2014, 12:02pm UTC](https://forum.dynamobim.com/t/dynamo-list-sorting-question/24/12 "2014-06-24T12:02:09Z")

</div>

FYI, here’s the info on updating Python definitions for 0.7.0. For non-Revity Python definitions, it’s mostly a case of changing the input statements.

[https://github.com/DynamoDS/Dynamo/wiki/Python-0.6.3-to-0.7.x-Migration](https://github.com/DynamoDS/Dynamo/wiki/Python-0.6.3-to-0.7.x-Migration)
