Once the records are sorted, you can use either a looping Set Field or Replace Field Contents. I suggest you use a loop if your solution is multi-user because you can test for record locking properly.
Go to Layout [ layout based upon this table ] ... or only use a button on this layout to fire this script
Show All Records
Sort [ Restore ; No dialog ]
Go to Record/Request/Page [ First ]
Set Error Capture [ On ]
Set Field [ yourTable:: Rank ; Get ( RecordNumber ) ]
If [ Get ( LastError ]
Set Variable [ $error ; $error & yourTable::uniqueID & "|" & Get ( RecordNumber ) & ¶
Go To Record/Reqest/Page [ next ; exit fter last ]
If [ not IsEmpty ( $error ) ]
Show Custom Dialog [ $error ]
If a record is locked because another User is modifying it, this displays the primary key and rank number in the custom dialog so you can manually set them when the record is free (write the information down before saying OK to the custom dialog; once the script finishes, the script variable will disappear). You can also write to global variable so value persists until you close or you can write to a field. Also note that Custom Dialog won't display but a few lines. Usually, there would only be one entry and it is easy to write it down at the time or you can search for blank Ranks. But writing to multiline and saving that information provides good protection for capturing fields which won't properly set because of record locking, paraticularly with many Users or on large record sets or with complex scripts.
You can also use Replace Field Contents ... however, even though you can test for locked records, this script-step will only tell you that you received a 201 (record lock) at the end so you cannot capture which record was locked. Only use Replace Field Contents if no Users at the time or if you can search for unset records afterwards.
UPDATE: Back up before running any new process.