# Automating sorting for Screenplay scene numbers

Hi,

I use Airtable to track information for film and television production.

I frequently need to sort a list of scenes by scene number, but scene numbers often have letters added to them, which screws up the sorting.

Example list, which I would want to sort in this order would be:

Sc. Number:
1
2
3
A4
4
5
A5
B5
C5
5
6
6A
6B
7pt1
7pt2
8
A9
9
10

The logic (for those unfamiliar with scene numbering) is that once a script is officially released to the crew, the scene numbers are locked. Then, when scenes are added, they add letters to indicate the scene. So if itâ€™s a scene that comes before scene 6, it would be A6 (and if there are more than one scene added, it would be B6, C6, etc) If the scene is added after scene 6, it would be 6A, 6B, 6C, etc.

And then sometimes a scene is split up for production. Often when there is a phone call happening itâ€™s only written as one scene, but when filming, both sides of the call are filmed, so the scene gets split into parts. Ex: 7pt1, 7pt2.

And it can be a combination of these things. So you could have a scene called A9pt1, A9pt2 A9pt3â€¦

SO, what Iâ€™m trying to do is get this inconsistent list to sort in the correct order without manual reordering in my table.

Any thoughts?

Thanks.

Welcome to the community, @Eric_Bryant1! This is a very interesting problem, and Iâ€™m neck deep in playing with a solution, but more info would be helpful.

When a letter prefix is addedâ€”e.g. A5, B5, etc.â€”whatâ€™s the greatest number of these that youâ€™ve seen? Has there ever been a case where a Z5 was added for example? Iâ€™m trying to figure out how many letters I need to account for, and Iâ€™m hoping it wonâ€™t need to go as far as Z.

Ignore my earlier question. I worked it out. Itâ€™s going to look like a mess Iâ€™m sure, but hereâ€™s the logic behind it:

• Start with the raw value of the scene number, ignoring any letter prefix/suffix and any part numbers
• If thereâ€™s a letter prefix, subtract the inverted position of that letter in the alphabet multiplied by .01; e.g. Z subtracts .01, Y subtracts .02, etc. This will put earlier letters before later ones, and put all such pre-scenes before their whole-scene-number equivalent, so that A4-?4 are all in order before 4.
• If thereâ€™s a letter suffix, do the same thing, only add .01 instead of subtracting it based on the letter. This ensures that 4A-4? are all after 4.
• Finally, if thereâ€™s a part number, multiply that by .01 and add that value

The result is a numeric sequence that uses these prefaces/suffixes to either add or subtract from the base scene number in a similar fashion to how they relate to each other based on your description (I hope!). Sorting by that formula field will put things in the proper order. Using your example, it appears to be working:

While Iâ€™ve set the formula field formatting to display the decimal values, you can leave it on integer display mode and it wonâ€™t affect the actual sorting. Still, seeing the values is helpful to ensure that the order is correct.

Hereâ€™s the formula, which relies heavily on regular expressions to find and extract specific pieces. Replace all instances of â€śSceneâ€ť with your actual scene name field.

``````IF(Scene,
VALUE(REGEX_EXTRACT(Scene, "[A-Z]?\\d*"))
- IF(REGEX_MATCH(Scene, "^[A-Z]"), FIND(REGEX_EXTRACT(Scene, "^[A-Z]"), "ZYXWVUTSRQPONMLKJIHGFECDBA") * .01)
+ IF(REGEX_MATCH(Scene, "(?:[^A-Z]\\d*)([A-Z])(?:.*)"), FIND(REGEX_EXTRACT(Scene, "(?:[^A-Z]\\d*)([A-Z])(?:.*)"), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") * .01)
+ IF(REGEX_MATCH(Scene, "pt.*"), VALUE(REGEX_EXTRACT(Scene, "(?:pt)(\\d*)")) * .01)
)
``````
1 Like

That is incredible, @Justin_Barrett! I have no idea how you figured that out!

Note that there is a small typo in your last RegEx function on the second line, where the letter â€śRâ€ť is mistakenly listed as a â€śUâ€ť.

Thanks, Scott! I blasted my way through typing that string out, so Iâ€™m not surprised that thereâ€™s a typo. I was a lot more careful in proofreading the other one (which, BTW, doesnâ€™t need to be reversed if I change the math slightly, but thatâ€™s how I pictured it, so thatâ€™s how I wrote it).

Maybe Iâ€™ll add this to my video production list to break it down in greater detail. I just need to figure out when to tackle that listâ€¦

1 Like

In theory it could go as far as Z, but iâ€™ve never seen it. If a screenplay needed that many revisions, Iâ€™d say itâ€™s back to the drawing board.

1 Like

Thank you so much @Justin_Barrett ! This is such an elegant solution.

Iâ€™m going to try to better understand your formula in the hopes of furthering my own knowledge.

After I saw this I realized that there is a scenario that I had forgot to mention. And that is SOMETIME, they might insert a Scene between A10 and 10. And they would call that AA10. Another scene inserted would be callee AAA10.

Iâ€™m going to try to use the logic if your formula to figure out how to accommodate that scenario. Itâ€™s rare, but I feel like it would be wise to cover that possibility as well.

Thanks again for the great solution.

@Eric_Bryant1 Itâ€™s possible to extend the logic that I used to cover those AA and AAA scenarios, but it would make the formula even more unwieldy than it already is. In short, working with a single alpha prefix only requires one of those long lines (currently line 3 of the above formula). Working with two or three means first counting the letters, and then executing a different branch depending on that count, with the calculation logic repeated (with slight changes) based on the count; i.e. once for a single letter, twice for two letters, and three times for three letters. That kind of extrapolation would be a lot easier if formulas were as flexible as scripts, which support recursive logic, iteration, etc.

In that light, another way to pull this off would be to use a manually-triggered Scripting app script (I wouldnâ€™t recommend doing this via automation) that you run whenever a new scene is added, which could do all the math and output the proper number into a `{Sort}` field. Such a script could use the advantages mentioned above to support any length of prefix, suffix, etc., and do it much more efficiently.

You could also just disable the sort if those edge cases come along and drag the record into position among the previously-auto-sorted scenes.

And neither will Justin two years from now when he searches for answers and discovers his own solution is the answer.

This topic was solved and automatically closed 15 days after the last reply. New replies are no longer allowed.