Example: How to sorting field with Label and separator : commas, semicolon, double slash `\\`

Sorting Composer Fields with Labels and Separators

This document provides ready-to-use Mp3tag formatting actions for sorting fields that contain labels (for example Composed:, Music:, Lyrics:, Written:) and different separators such as commas, semicolons, and true multi-value \\ separators.

The formulas below have been tested with Convert > Tag - Tag and/or Format value actions.

Important
Always test on copies or backed-up files first. Some formulas intentionally convert between a true multi-value field and a single text value.


1. Sorting a Single Field with Label and Commas

Input

Composed: Kasa Allah, Benny Golson, Stuart Gorrell, Hoagy Carmichael

Output

Composed: Benny Golson, Hoagy Carmichael, Kasa Allah, Stuart Gorrell

Without label

For a true single-value text field whose items are separated by commas:

$sort(%composer%,', ')

With label Composed:

Composed: $sort($regexp(%composer%,^Composed:\s*,),', ')

Logic Flow

  • %composer% β†’ reads the single text value.
  • $regexp(...,^Composed:\s*,,) β†’ removes the initial Composed: label and any following spaces.
  • $sort(...,', ') β†’ sorts the comma-separated names alphabetically.
  • Composed: β†’ is added back once at the beginning.

The older form $sort($meta_sep(composer,),', ') can also work when COMPOSER contains only one physical value, but %composer% is clearer here because this section concerns a single-value field.


2. Multi-Value Field Variant (\\ separator)

Example input

Composed: Kasa Allah\\Benny Golson\\Stuart Gorrell\\Hoagy Carmichael

Action

Composed: $sort($replace($meta_sep(composer,\\),Composed: ,),'\\')

Output

Composed: Benny Golson\\Hoagy Carmichael\\Kasa Allah\\Stuart Gorrell

Logic Flow

  • $meta_sep(composer,\\) β†’ collects all values of the multi-value COMPOSER field using \\ as the working separator.
  • $replace(...,Composed: ,) β†’ removes Composed: .
  • $sort(...,'\\') β†’ sorts the values alphabetically using \\ as the delimiter.
  • Composed: β†’ is re-added once at the beginning.

Assumption

This formula assumes that Composed: appears only as the initial label and is not part of a person's name or another value.


3. Case-Insensitive Label Variants

Mp3tag's fourth $regexp() parameter can enable case-insensitive matching:

$regexp(text,expression,replacement,1)

The final 1 means that the regular-expression match ignores capitalization.

Therefore the same expression can match:

Composed:
composed:
COMPOSED:
CoMpOsEd:

Single field β€” commas

Composed: $sort($regexp(%composer%,^composed:\s*,,1),', ')

Multi-value β€” \\

Composed: $replace($sort($regexp($meta_sep(composer,|),^composed:\s*,,1),'|'),|,\\)

Logic Flow for the multi-value version

$meta_sep(composer,|)
        ↓
Temporarily joins all values with |

$regexp(...,^composed:\s*,,1)
        ↓
Removes the label regardless of capitalization

$sort(...,'|')
        ↓
Sorts the values

$replace(...,|,\\)
        ↓
Restores the true multi-value separator

Temporary separator rule
The character | is used only as a safe temporary delimiter. Use it only when | does not occur in the actual tag data.


4. Semicolon Separator Variants

Single field already separated by ;

If the field is a single text value whose names are separated by semicolons:

Composed: $sort($regexp(%composer%,^composed:\s*,,1),'; ')

Example:

Composed: Kasa Allah; Benny Golson; Stuart Gorrell; Hoagy Carmichael

becomes:

Composed: Benny Golson; Hoagy Carmichael; Kasa Allah; Stuart Gorrell

Convert multi-value \\ input to a single semicolon-separated value

Composed: $replace($sort($regexp($meta_sep(composer,|),^composed:\s*,,1),'|'),|,'; ')

This formula intentionally converts:

A\\B\\C

into:

A; B; C

So the result is no longer a true Mp3tag multi-value field. It is one text value containing semicolon-separated items.


5. Sorting Multi-Value COMPOSER with Two Fixed Labels (Music / Lyrics)

This version is positional.

It works only when:

  • exactly two COMPOSER values are expected;
  • value 0 is always Music:;
  • value 1 is always Lyrics:.

Input

Music: Tommaso Demuro, Steve Swallow, David Pritchard\\Lyrics: Gary Burton, Bill Goodwin, Apple Green

Output

Music: David Pritchard, Steve Swallow, Tommaso Demuro\\Lyrics: Apple Green, Bill Goodwin, Gary Burton

Action

Music: $sort($regexp($meta(composer,0),^music:\s*,,1),', ')\\Lyrics: $sort($regexp($meta(composer,1),^lyrics:\s*,,1),', ')

Logic Flow

  • $meta(composer,0) β†’ reads the first physical COMPOSER value, assumed to be the Music block.
  • $regexp(...,^music:\s*,,1) β†’ removes Music: case-insensitively.
  • $sort(...,', ') β†’ sorts the names inside the Music block.
  • Music: β†’ is added back before that sorted block.

Then:

  • $meta(composer,1) β†’ reads the second physical COMPOSER value, assumed to be the Lyrics block.
  • $regexp(...,^lyrics:\s*,,1) β†’ removes Lyrics: case-insensitively.
  • $sort(...,', ') β†’ sorts the names inside the Lyrics block.
  • Lyrics: β†’ is added back before that sorted block.
  • \\ β†’ keeps the two values physically separate as a true multi-value field.
  • ,1 β†’ enables case-insensitive matching in each $regexp().

$meta(composer,0) and $meta(composer,1) select values by position, not by label content.


6. Sorting Music/Lyrics When Their Positions Are Not Fixed

This version solves two tasks at the same time:

  1. it normalizes the label order to the fixed canonical order Music β†’ Lyrics;
  2. it sorts the names alphabetically inside each label;
  3. label recognition is case-insensitive;
  4. it works whether the input is Music\\Lyrics or Lyrics\\Music.

Assumptions

  • exactly two COMPOSER values are expected;
  • one begins with Music:;
  • the other begins with Lyrics:;
  • the label starts at the first character of each value.

Action

$if($eql($left($meta(composer,0),6),Music:),Music: $sort($regexp($meta(composer,0),^music:\s*,,1),', ')\\Lyrics: $sort($regexp($meta(composer,1),^lyrics:\s*,,1),', '),Music: $sort($regexp($meta(composer,1),^music:\s*,,1),', ')\\Lyrics: $sort($regexp($meta(composer,0),^lyrics:\s*,,1),', '))

Logic Flow

$left($meta(composer,0),6)

reads the first six characters of value 0.

Then:

$eql(...,Music:)

asks whether the first value begins with Music:.

If YES:

meta 0 β†’ Music
meta 1 β†’ Lyrics

If NO:

meta 1 β†’ Music
meta 0 β†’ Lyrics

In both cases the field is reconstructed as:

Music: ...\\Lyrics: ...

Therefore the formula is normalizing, not simply positional.

Example A β€” original order

Music: Tommaso Demuro, Steve Swallow, David Pritchard\\Lyrics: Gary Burton, Bill Goodwin, Apple Green

Example B β€” swapped input

Lyrics: Gary Burton, Bill Goodwin, Apple Green\\Music: Tommaso Demuro, Steve Swallow, David Pritchard

Both produce:

Music: David Pritchard, Steve Swallow, Tommaso Demuro\\Lyrics: Apple Green, Bill Goodwin, Gary Burton

This is a fixed/canonical label order, not an alphabetical label order. Alphabetically, Lyrics comes before Music.


7. Automatic Sorting of Two Labeled Multi-Values: Labels + Names

This version performs two independent alphabetical sorts:

  1. alphabetical sorting of the names inside each label;
  2. alphabetical sorting of the complete labeled blocks themselves.

It is designed for exactly two labeled multi-values.

Action

$replace($sort($regexp($meta(composer,0),'^([^:]+):.*','$1'): $sort($regexp($meta(composer,0),'^[^:]+:\s*',,1),', ')|$regexp($meta(composer,1),'^([^:]+):.*','$1'): $sort($regexp($meta(composer,1),'^[^:]+:\s*',,1),', '),'|'),|,\\)

What this version does

  • automatically extracts each label;
  • does not require the literal words Music or Lyrics in the formula;
  • removes each label before sorting the associated names;
  • sorts the names alphabetically;
  • rebuilds each complete Label: names block;
  • sorts the two complete blocks alphabetically by their leading label text;
  • restores the true multi-value \\ separator;
  • works regardless of which labeled block is initially value 0 or value 1;
  • label extraction itself does not depend on capitalization because no specific label word is hard-coded.

Logic Flow

The following part:

$regexp($meta(composer,0),'^([^:]+):.*','$1')

automatically extracts everything before the first colon.

For example:

Music

or:

Lyrics

This part:

$regexp($meta(composer,0),'^[^:]+:\s*',,1)

removes the complete initial Label: portion, leaving only the names.

Then:

$sort(...,', ')

sorts the comma-separated names inside that block.

The same operations are performed independently on $meta(composer,1).

At this stage the two reconstructed blocks are temporarily joined with |, for example:

Music: David Pritchard, Steve Swallow, Tommaso Demuro|Lyrics: Apple Green, Bill Goodwin, Gary Burton

Then:

$sort(...,'|')

sorts the complete blocks alphabetically according to the text at the beginning of each block:

Lyrics: ...
Music: ...

Finally:

$replace(...,|,\\)

converts the temporary delimiter back into the true Mp3tag multi-value separator.

Why the regex patterns are quoted

These two patterns contain square brackets:

'^([^:]+):.*'
'^[^:]+:\s*'

In Mp3tag Format Strings, [ and ] have their own special meaning. Wrapping these regular-expression patterns in single quotes prevents Mp3tag from interpreting the square brackets before the pattern reaches the regex engine.

Example 1

Input:

Music: Tommaso Demuro, Steve Swallow, David Pritchard\\Lyrics: Gary Burton, Bill Goodwin, Apple Green

Output:

Lyrics: Apple Green, Bill Goodwin, Gary Burton\\Music: David Pritchard, Steve Swallow, Tommaso Demuro

Example 2 β€” swapped input

Input:

Lyrics: Gary Burton, Bill Goodwin, Apple Green\\Music: Tommaso Demuro, Steve Swallow, David Pritchard

Output:

Lyrics: Apple Green, Bill Goodwin, Gary Burton\\Music: David Pritchard, Steve Swallow, Tommaso Demuro

Assumptions and limitations

  • Exactly two physical COMPOSER values are processed because the formula explicitly references $meta(composer,0) and $meta(composer,1).
  • Each physical value must begin with a label followed by a colon, for example Music: or Lyrics:.
  • The label itself must not contain an additional colon before the separator colon.
  • Names inside each block are expected to be separated by , .
  • The temporary | character must not occur in the original tag data.
  • Sorting behavior is provided by Mp3tag's $sort() function; this formula does not add a separate case-sensitivity option to $sort().

8. Function Reference Used in These Examples

$sort(x,y)

Sorts the segments in x using y as the delimiter.

Examples:

$sort(%composer%,', ')
$sort(...,'\\')
$sort(...,'|')

$meta(field,n)

Returns the n-th physical value of a multi-value field. Indexing starts at 0.

$meta(composer,0)
$meta(composer,1)

$meta_sep(field,separator)

Collects all physical values of a multi-value field and joins them temporarily with the chosen separator.

$meta_sep(composer,\\)
$meta_sep(composer,|)

$regexp(text,expression,replacement,1)

Runs a regular-expression replacement. The optional fourth parameter 1 enables case-insensitive matching.

$regexp(%composer%,^composed:\s*,,1)

$replace(text,search,replace)

Performs a literal replacement and is useful when regular expressions are unnecessary.

$replace($meta_sep(composer,\\),Composed: ,)

$eql(x,y)

Compares two strings. In the formulas above it is used to detect whether value 0 begins with Music:.


9. Choosing the Correct Formula

Use the formula that matches the real structure of the field, not only what it looks like in Mp3tag.

Scenario Recommended section
One text value, names separated by commas 1
True multi-value field separated by \\ 2
Label capitalization may vary 3
Single text value separated by semicolons 4
Convert true multi-values to one semicolon-separated string 4
Exactly two values, fixed Music then Lyrics positions 5
Exactly two values, Music / Lyrics positions may be swapped, but output must always be Music β†’ Lyrics 6
Exactly two generic labeled values; both labels and names must be sorted alphabetically 7

Notes

  • All examples are intended for Convert > Tag - Tag or Format value actions.
  • When adapting the examples to another field, replace the field consistently everywhere. For example, changing COMPOSER to another field may require changing %composer%, $meta(composer,...), and $meta_sep(composer,...).
  • A displayed \\ separator represents separate physical values in a true multi-value field; a comma or semicolon inside one value is only text unless the field is explicitly split into multiple values.
  • Temporary separators such as | must not occur in the source data.
  • Re-running the normalizing formulas on already normalized data should leave the logical result unchanged, provided the stated assumptions are respected.
  • Always back up your files before using bulk actions.
  • If you find errors or want to improve the formulas, feel free to edit this document.
  • Thanks, ChatGPT, for the help.

Did ChatGPT also mention that the results are currently sorted by first name?

For your example:

Composed: Kasa Allah, Benny Golson, Stuart Gorrell, Hoagy Carmichael

I would expect the following output if sorted by last name:

Composed: Kasa Allah, Hoagy Carmichael, Benny Golson, Stuart Gorrell

Even better, it would be great if users could choose the preferred sort order themselves.

Just as a note:
I haven’t read through your rather lengthy post in full, and I haven’t tested this any further myself.

The sorting is by name. Regarding user preferences, I agree with you; my tags come from Discogs, where they are sorted by name. As for the length of the post, I simply listed variations of the scenarios, which in some cases can be adapted to other fields.