Define Order of Objects by X, Y Values In List

Hi! I have been working on script for my group to create layouts and viewports after some awesome nodes in 2025 were added. I have been able to create the views, but the order of the views has been inconsistent. My thought is to add a property set value to each viewport AutoCAD Block Reference that I place in model space to eventually tell Dynamo what order to create the viewports in each layout tab. Ideally, I want the order to start at the top left, and work right, then down a line and work right again. Top left to bottom right. I will attach my script that I am using to create the viewports, but that creates it in an inconsistent order, a script I am testing with that will apply a value to a “Viewport_Order_Number”, but I am not sure how to organize this in a way that it will place the correct number in each property set. I am also attaching my CAD file with the keyplan/viewport layout in model space. Thanks as always for any and all feedback!


LayoutCreation_2025_V2.dyn (99.4 KB)
ViewportPropertySet.dyn (19.2 KB)
Viewport Test File.dwg (1.7 MB)

Hi @jbrunkhorst,

The List.SortByKey is the node you’ll want here. The list is what you want to sort (the views) and the keys are what is used to sort them. In your case, the keys would be the coordinates or view numbers, whatever you decide.

Example here if you need:

Ah, this gets my coordinates in the correct order, thank you! Is there a way to get this order of points (from the screenshot below) to resort the list of coordinates from earlier in the script that are used to create the viewports now?

@zachri.jensen
I am did some more digging into this, this morning and I thought I had it. But Can someone help me understand why when I use the List.SortByKey node that it doesn’t keep the order from the sortedKeys List? Notice in my screenshots below that the Y values from the first List.SortByKey it has 5 values at the 551.678 position, but then once I try to resort my coordinate systems, it only groups 3 of them at the front of the list and pushes the other coordinates with the 551.678 to later in the list.


I found a way to get this organized in a way I want with a python script. That script is below for those who are curious in the future:
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Function to sort coordinate systems by their y positions in descending order

and then by their x positions in ascending order for each group of identical y values

def sort_coordinate_systems(coord_systems):
# Sort by y in descending order, then by x in ascending order
sorted_coord_systems = sorted(coord_systems, key=lambda cs: (-cs.Origin.Y, cs.Origin.X))
return sorted_coord_systems

Example usage

Assuming you have a list of coordinate systems

coordinate_systems = IN[0] # Input list of coordinate systems

Sort the coordinate systems

sorted_coordinate_systems = sort_coordinate_systems(coordinate_systems)

Output the sorted list

OUT = sorted_coordinate_systems