# Sort field content alphabetically with external workaround for missing $sort function

**URL:** https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901
**Category:** Off-Topic
**Created:** [March 20, 2025, 8:33am UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901 "2025-03-20T08:33:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)
#### Post date: [March 20, 2025, 8:33am UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/1 "2025-03-20T08:33:12Z")

</div>

From time to time users ask for a solution to sort the contents of fields, like [here](https://community.mp3tag.de/t/sort-alphabetically-wthin-a-tag/44129) or [here](https://community.mp3tag.de/t/script-to-alphabetize-a-field/61706) or [here](https://community.mp3tag.de/t/how-to-sort-string-values-in-a-field-regex/65925).

~~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:
> 
> ```auto
> @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
> 
> ```auto
> 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"
> 
> ![image](https://community.mp3tag.de/uploads/default/original/3X/7/6/766b5256d5ddeb6cd0974bbdb17c5461835b2b6a.png)
> 
> 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:
> 
> ![image](https://community.mp3tag.de/uploads/default/original/3X/e/a/ea319be8f6bb1066c01471d646a93f73d39eccd5.png)
> 
> 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:  
> ![image](https://community.mp3tag.de/uploads/default/original/3X/e/2/e2fa8adc9d354ecd56a113dba9839c21dbb0d404.png)  
> ![image](https://community.mp3tag.de/uploads/default/original/3X/3/a/3a3bd2b1910b5904a095666899c5f8c221b1e1de.png)  
> ![image](https://community.mp3tag.de/uploads/default/original/3X/8/e/8ea36a3c61c1f7e96ca721ed3349b22ddd4385be.png)
> 
> 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](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/3).

---

<div class="post-metadata">

### Author: ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)
#### Post date: [March 20, 2025, 8:41am UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/2 "2025-03-20T08:41:07Z")

</div>

What I forgot:

As Mp3tag can not delete files with an Action, you need to cleanup the no more needed `%title%_SortedContent.txt` manually.  
It should be easy to sort your files by file type or search for `_SortedContent.txt` and then delete the no more needed files.

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [March 20, 2025, 11:58am UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/3 "2025-03-20T11:58:46Z")

</div>

> [@LyricsLover](#):
>
> This would all be much simpler if we had a `$sort(x,y,z)` script command.

I've added a `sort` functions with [Mp3tag v3.28i](https://community.mp3tag.de/t/455), where you can sort a given string that is delimited by a given delimiter, e.g., `$sort('c;a;b',';')`

---

<div class="post-metadata">

### Author: ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)
#### Post date: [March 20, 2025, 12:17pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/4 "2025-03-20T12:17:50Z")

</div>

That was fast! Thanks.

The new `$sort` needs some little fine-tuning:  
`Vocal Trance; Trance; Dream; House;`  
becomes:

 ![image](https://community.mp3tag.de/uploads/default/original/3X/3/0/3021abd747c254dc2a9597f432922c204792ca10.png)

As you can see in the above PowerShell-script, the last (mostly not needed) delimiter needs to be removed first, otherwise it will result in a double delimiter somewhere.

Using only a semicolon (without space) shows:

 ![image](https://community.mp3tag.de/uploads/default/original/3X/9/4/9426a5fccae5216338919f653d1aba97c34c0f52.png)

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [March 20, 2025, 1:11pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/5 "2025-03-20T13:11:37Z")

</div>

I don't want to be a feature creep ...  
Looking at this suggestion

> [@Suggestion - Sort function for multivalue tag-fields](https://community.mp3tag.de/t/suggestion-sort-function-for-multivalue-tag-fields/10743):
>
> I think that Mp3tag needs a sort function (ascending, descending) for sorting a multivalue meta tag-field. DD.20100830.0849.CEST

the function is missing (very hard word) the switch for ascending or descending sort order.

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [March 20, 2025, 2:06pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/6 "2025-03-20T14:06:18Z")

</div>

> [@LyricsLover](#):
>
> the last (mostly not needed) delimiter needs to be removed first, otherwise it will result in a double delimiter somewhere.

Not sure if I want to go in that direction, because strictly speaking, the delimiter is "`; `" (= semicolon-space) and not "`;`" (semicolon).

Treating the trailing semicolon as delimiter works in the example, but might not be intended in other contexts.

An possible solution in such cases is to use

`$sort($trim('Vocal Trance; Trance; Dream; House;',';'),'; ');`

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [March 20, 2025, 2:17pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/7 "2025-03-20T14:17:14Z")

</div>

> [@ohrenkino](#):
>
> the function is missing (very hard word) the switch for ascending or descending sort order.

Thanks, I've added a note to my internal wish list.

If someone provides a practical real-life example demonstrating the need for this, I'll consider adding a third parameter for the sort order.

---

<div class="post-metadata">

### Author: ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)
#### Post date: [March 20, 2025, 2:33pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/8 "2025-03-20T14:33:53Z")

</div>

> [@Florian](#):
>
> An possible solution in such cases is to use
> 
> `$sort($trim('Vocal Trance; Trance; Dream; House;',';'),'; ');`

Personally, I can live with this workaround.  
Not sure about other users though.

I would use it this way to indicate which trailing semicolon has to be removed  
from  
`Vocal Trance; Trance; Dream; House;`  
to

 ![$trimRight](https://community.mp3tag.de/uploads/default/original/3X/3/8/3842209717101118b6881dc1452b8006f9702af0.png)

And combined with the new `$sort` command, I get the expected result as you stated.

I'm just not sure if other users may see the not so obvious difference in a leading space switching between position 0 and the words `Trance;` and `Vocal Trance` in this example:  
 ![$sort and $trim](https://community.mp3tag.de/uploads/default/original/3X/b/8/b84a12f3786a91c38e1556bba6e0cb4c222fd15a.gif)

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [March 20, 2025, 2:36pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/9 "2025-03-20T14:36:13Z")

</div>

Perhaps I should have praised the new function first.  
I just took a file with lyrics

```auto
"Xxx||To the Moon, Through the Stars 
There We'll Find a Place to Be
Close Your Eyes, 
All These Wonders in the Dark 
We Saw Them Shine
You and Me" 

```

and used `$sort($replace(%unsyncedlyrics%,Xxx||,),$char(13)$char(10))`  
and got

```auto
"All These Wonders in the Dark 
Close Your Eyes, 
There We'll Find a Place to Be
To the Moon, Through the Stars 
We Saw Them Shine
You and Me"

```

So it works also on more or less any list, it seems.

---

<div class="post-metadata">

### Author: ![juvalu](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@juvalu](https://community.mp3tag.de/u/juvalu)
#### Post date: [April 23, 2025, 12:51pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/10 "2025-04-23T12:51:08Z")

</div>

For anyone who needs it "Sort the contents of the field alphabetically separated by \\"

example field: PERFORMER

text in random order

```auto
Strings: The London Session Orchestra\\Score: William Jones\\Engineer [Assistant]: Howard Bargroff, Max Bloom\\Vibraphone [Vibes]: Frank Ricotti\\Written-By: C. Womack, G. Noble, L. Womack\\Leader [Orchestral Leader]: Gavyn Wright\\Piano: Pete Wingfield\\Programmed By [Programming]: Mel Wesson\\Guitar: Taj Wyzgowski\\Drums: Rupert Brown\\Arranged By [Arrangement]: Nick Ingam\\Engineer: Martin Hayles\\Flute, Electronic Wind Instrument: Phil Todd\\Percussion: Miles Bould\\Backing Vocals [Background Vocals]: Linda Gerald, Lorraine McIntosh, Tee Green\\Producer: Michael Peden

```

Convert tag-tag

```auto
FIELD: PERFORMER

Format string:
$sort($meta_sep(performer,\\\\),'\\')

```

preview

```auto
Arranged By [Arrangement]: Nick Ingam\\Backing Vocals [Background Vocals]: Linda Gerald, Lorraine McIntosh, Tee Green\\Drums: Rupert Brown\\Engineer [Assistant]: Howard Bargroff, Max Bloom\\Engineer: Martin Hayles\\Flute, Electronic Wind Instrument: Phil Todd\\Guitar: Taj Wyzgowski\\Leader [Orchestral Leader]: Gavyn Wright\\Percussion: Miles Bould\\Piano: Pete Wingfield\\Producer: Michael Peden\\Programmed By [Programming]: Mel Wesson\\Score: William Jones\\Strings: The London Session Orchestra\\Vibraphone [Vibes]: Frank Ricotti\\Written-By: C. Womack, G. Noble, L. Womack

```

works perfectly thanks Florian!

---

<div class="post-metadata">

### Author: ![7stones](https://community.mp3tag.de/user_avatar/community.mp3tag.de/7stones/32/17075_2.png) [@7stones](https://community.mp3tag.de/u/7stones)
#### Post date: [April 24, 2025, 12:25pm UTC](https://community.mp3tag.de/t/sort-field-content-alphabetically-with-external-workaround-for-missing-sort-function/67901/11 "2025-04-24T12:25:22Z")

</div>

> [@juvalu](#):
>
> ```auto
> FIELD: PERFORMER
> 
> Format string:
> $sort($meta_sep(performer,\\\\),'\\')
> 
> ```

Great! Just a tip, you can simplify to

`$sort($meta_sep(performer,\\),\\)`
