From time to time users ask for a solution to sort the contents of fields, like here or here or here.
Currently, we don't have any $sort function for this task up to Mp3tag version v3.28h.
So I wrote an external workaround for this.
You need
a) a CMD-Batchfile, I call it SortTheContentIn.cmd
and
b) a PowerShell-Script: SortStringBySeparator.ps1
and
c) a installed and working Windows-PowerShell, at least in version v2.0
and
d) a new Mp3tag Tools entry, I call it "Split content by separator, Sort the content and Write it into a new file"
and
e) a new Mp3tag Action to import the sorted field content
The CMD-Batchfile SortTheContentIn.cmd contains the call of the PowerShell-Script, including the ExecutionPolicy Bypass to start it:
@ECHO OFF
SET InputString=%1
SET Delimiter=%2
SET OutputFile=%3
SET ps="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
SET psscript=SortStringBySeparator.ps1
REM PowerShell-Skript aufrufen, Bypass ExecutionPolicy
%ps% -ExecutionPolicy Bypass -File %psscript% -InputString %InputString% -Delimiter %Delimiter% -OutputFile %OutputFile%
The PowerShell-Script: SortStringBySeparator.ps1 contains
param (
[string]$InputString, # Der zu sortierende, komplette String
[string]$Delimiter, # Das Trennzeichen
[string]$OutputFile # Der Name der Ausgabedatei
)
# String in ein Array aufteilen, dabei abschliessendes Trennzeichen entfernen, leere Elemente filtern, sortieren, Duplikate entfernen und wieder zusammenfügen
$output = ($InputString.TrimEnd($Delimiter) -split [regex]::Escape($Delimiter) | Where-Object { $_ -ne "" } | Sort-Object | Select-Object -Unique) -join $Delimiter
# Ergebnis in die Ausgabeddatei schreiben
Set-Content -Path $OutputFile -Value $output
# Bestätigungsausgabe
Write-Output "Der sortierte String wurde in die Datei '$OutputFile' geschrieben."
# 5 Sekunden auf Tastendruck warten oder nach Ablauf automatisch beenden
TIMEOUT /T 5
This script reads the given String, the delimiter and the name of the outputfile.
The split and sorted output is written in the outpufile name.
The new Mp3tag Tools entry "Split content by separator, Sort the content and Write it into a new file" needs two values:
a) The complete path to the above CMD-Batchfile SortTheContentIn.cmd
and
b) 3 Parameters:
The fieldname (like MOVEMENTNAME containing the unsorted String)
The delimiter/separator character (like a semicolon followed by a space)
The ouptut filename for the sorted string (like %title%_SortedContent.txt)
I recommend to enclose every of these 3 parameters in double quotes " "
Don't forget to select "for all selected files"
Then we need the final step to import the sorted content back in the tag field.
I name the Action "Import sorted field content".
The type of Action is "Import text file".
You can choose any target Field you like. To be 200% sure that it will work correctly, I recommend choosing a custom field like MVT_TMP1. This allows you to double-check that everything is working correctly without overwriting your original MOVEMENTNAME content.
As the Filename you must use the same output filename (such as %title%_SortedContent.txt) that you selected in the Tools definition:
You can now select your files in the File List of Mp3tag, right-click and choose Tools and select Split content by separator, Sort the content and Write it into a new file. This write an output file for every track in the same directory.
With the Action "Import sorted field content" you can re-import the sorted output.
All these steps will lead to these results:
![]()


If you are absolutely sure that everything is working as expected, you can copy the contents of the temporary MVT_TMP1 into your original field name. Use an Action or Convert Tag->Tag for this task.
You can then delete the temporary MVT_TMP1.
This would all be much simpler if we had a $sort(x,y,z) script command.
x would be the import string/the original field name,
y would be the delimiter
and
z would be the destination field.
Omitting z would write the sorted contents into the original field.
This is not true anymore!
Since Mp3tag v3.28i we now can use the new available $sort function.





