[SNIPPET] Sort Name

All about user-defined episode / movie / file name format expressions
Post Reply
User avatar
rednoah
The Source
Posts: 24032
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

[SNIPPET] Sort Name

Post by rednoah »

Sometimes it may be useful to put the The or an A that many movie names begin with (e.g. The Avengers) at the end of the name (i.e. Avengers, The) to improve lexical sort order. You can do that in your custom format, either via the String.sortName() helper method or some custom regex replace.


The Avengers => Avengers

Format: Select all

{ n.sortName() }

The Avengers => Avengers, The

Format: Select all

{ n.sortName('$2, $1') }

The Avengers => A

Format: Select all

{ n.sortName()[0] }

Custom Regex Replace:

Format: Select all

{ n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /$2, $1/) }

:!: Note that most software expects the file name and the database entry to match exactly. If you mess up the file name like this, then FileBot and any other 3rd party software may have trouble making sense of the file name in the future. :!:
:idea: Please read the FAQ and How to Request Help.
DigidocTN
Posts: 5
Joined: 10 Jul 2025, 12:45
Location: N Georgia, near Chattanooga

Re: [SNIPPET] Sort Name

Post by DigidocTN »

Again, almost perfect. { n.replaceFirst(/^(?i)(The|A|An)\s(.+)/, /$2, $1/) }

I have a show: The Gabby Hayes Show (1956) and using the code above, I get: Gabby Hayes Show (1956), The.

Is it possible to insert The before the date (1956) instead of after?
User avatar
rednoah
The Source
Posts: 24032
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: [SNIPPET] Sort Name

Post by rednoah »

:idea: If you use TheMovieDB then the series name {n} will never include the series year and thus { n.sortName('$2, $1') } will always work as expected.

:idea: If you use TheTVDB then the series name {n} may include the series year as part of the series name. In this case we can use replaceTrailingBrackets() to remove the (1956) from the series name:

Format: Select all

{ n.replaceTrailingBrackets().sortName('$2, $1') } 
:idea: Please read the FAQ and How to Request Help.
Post Reply