Select to view content in your preferred language

Field Calculator — Sort values, partitioned by GROUP_ID field

306
3
08-22-2024 10:42 AM
Labels (1)
Bud
by
Esteemed Contributor

ArcGIS Pro 3.3.1; file geodatabase

GROUP_ID VALUES 
1 0
1 1
1 0

2 0
2 0

3 1
3 0
3 1

I want to rearrange the VALUES data so that the values are sorted descending, sorted within each GROUP_ID partition.

GROUP_ID VALUES 
1 1
1 0
1 0

2 0
2 0

3 1
3 1
3 0

How can I do that using the Field Calculator?

I’m aware of the Sort geoprocessing tool. But I don’t want to use Sort because it outputs a separate FC. I want to update the original FC directly. And I would prefer not to use the Sort tool, join it back to the original FC, and then do a filed calculation to bring in the joined/sorted values.

I don’t want to sort the rows, I want to edit the values and leave the row order as-is.

3 Replies
JoshuaBixby
MVP Esteemed Contributor

Why does it have to be Calculate Field?  This is much more straight-forward with cursors.

0 Kudos
Bud
by
Esteemed Contributor

I’m inexperienced with Python, so I assumed a field calculation would be easiest. I hadn’t considered a Python cursor script in the Python Window as an option. But if that’s easier, then it would be fine.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

With Calculate Field, there is no way to ensure row order, truly ensure it.  So, doing order-dependent row operations is kludgy at best and generally error prone.  With cursors, you can specify an ORDER BY statement to ensure row order, or you can loop through a cursor twice using a dictionary to store and update values.  If someone else doesn't offer up some code, I will take a look in a day or so.