Calculate Unique ID's based on table sort

2040
10
Jump to solution
05-19-2020 08:04 AM
JeremyJones1
New Contributor II

Hi All.. So here is what I'm trying to do.  I know how to calculate Unique ID's.  What I'm trying to do is calculate them based the the sort of the table.  My data was randomly selected and on this field the table has been sorted.  I want to then calculate the Unique ID's based on this sort.  So my first record will get a 1 and so on.  What currently happens is the unique ID is created based on the object ID or FID not sure which causing my sort on my field to be invalid.  Is there a way to force the Unique ID to calculate based on the table sort?  Thanks

-Jeremy 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
10 Replies
JoeBorgione
MVP Emeritus

See if this approach works for you. In the example below, I use the arcpy.da.UpdateCursor with a sql clause to ORDER By (sort) by PARCEL which is a text field:

import arcpy
fc = r'C:\Temp\Junk.gdb\points'
id = 0
with arcpy.da.UpdateCursor(fc,'SortID', sql_clause=(None, 'ORDER BY PARCEL')) as cursor:
    for row in cursor:
        id+=1
        row[0] = id
        cursor.updateRow(row)

Here is a screen capture of my results:

That should just about do it....
JeremyJones1
New Contributor II

Thanks for the reply Joe!

0 Kudos
DanPatterson
MVP Esteemed Contributor
JeremyJones1
New Contributor II

Thanks Dan I was able to use your tools and do what I needed to do. 

0 Kudos
JoeBorgione
MVP Emeritus

Just out of courtesy, how about you mark Dan's reply as the answer.

That should just about do it....
0 Kudos
JeremyJones1
New Contributor II

Because I actually used his.  I Yours most likely works as well I just don't have experience with coding and modifying it.  If I could mark them both as correct I would have.

JoeBorgione
MVP Emeritus

That's cool! Dan Patterson needs the MVP points more than I do...   

That should just about do it....
DanPatterson
MVP Esteemed Contributor

actually... it is the "new Dan".  My old account got "retired and frozen" and I had to open a new one.  At level 5 now , only 140,000 to go till I am back where I was


... sort of retired...
0 Kudos
JoeBorgione
MVP Emeritus

You are telling me I have more points than you now!  That just made my day!!!

That should just about do it....
0 Kudos