Remove duplicate data and sort data within a single field


I am trying to write an action group focusing on the field (%CONTENTGROUP%). The field contains particular values separated by spaces (possible more than one), which I use to create smart folders in itunes. The existing values are:

$-$5 ($+single number. may not contain a number)
!-!5* (!+single number. may not contain number. may contain a trailing *)
A-Z (single capital letter)
[1]-[5] (single number in brackets. may be duplicates)
{A}-{Z} (single capital letter in braces. may be duplicates)
(Ax)-(Zy) (double capital letters in parentheses)

(single #. may be duplicates)

Some hypothetical examples:

ex1: $ [1] # [3] T [1] A {S} !2* [1]
ex2: !2 $3 [4] [2] [4] # (JU) {S} {P} {P} #
ex3: $1 ! [3] {S} (TT) # [1] [3] {P} T {B}

I would like to do the following with the action group:


  1. Remove any duplicate "words", single "symbols" and single letters.
  2. Sort the data within the field in this order:

$number (ascending)
!number (ascending)
Single LETTER (alphabetical)
[number] (ascending)
{LETTER} (alphabetical)
(Double LETTER) (alphabetical)

  1. Remove extra spaces

The desired results for my examples would be:

ex1: $ !2* A T [1] [3] {S} #
ex2: $3 !2 [2] [4] {P} {S} (JU) #
ex3: $1 ! T [1] [3] {B} {P} {S} (TT) #

I could use the following for the extra space removal, but I'm not sure whether it should run first or last:

Replace reg expression
(leave blank)

Replace reg expression
(leave blank)

Replace reg expression
(single space)

As far as the sorting and duplicate removal goes, I'm stumped.

Any help creating this would be most appreciated!


Well, to make your crazy adventure a short story ...
ad 1: Search the forum for a method using regular expression to remove duplicate items from an item list, there is one buried in the forum posts in the last one or two years.
ad 2: Mp3tag provides no sort method for text strings in the format of a delimiter separated item list.
ad 3: Already solved by yourself.

Maybe you can solve the processing of the complex contentgroup scheme by dividing the complex value into some dedicated helper tag-fields, so that each helper tag-field carries one set of data type, which possibly can be processed by Mp3tag functions.