Turning "Firstname Lastname" Album Artists into "Lastname, Firstname" Album Artist Sort

I have several different formulae for filing albums, based on whether to include the disc number, track artist, etc. They're all organized, at the top level, by the albumartistsort value or, if that would be the same as the albumartist, by that albumartist value. (For example, I could just use "Journey" or "Neutral Milk Hotel" as is.)

Frequently, however, the artist has a Firstname Lastname, such as "John Lennon". I would love to create an action that could swap that into "Lastname, Firstname" for sorting. I recognize that this would not be good as a default action (for example, I know that "Jethro Tull" or "Moxy Fruvous" would not get swapped), but in the many situations in which I see that I would have to do this by hand, I would love to have an action to do this automatically.

I'm pretty sure that this could be done with the tagging tools, but I haven't figured this out. Does anyone already have a formula to do this?

There are several solutions already available in this forum.
Please use the search function with words like
swap firstname lastname

And please be aware the many exceptions that exists beside group names, like artists with multiple parts in the name as "The Artist formerly known as Prince" or combinations like "Bob Marley & The Wailers".

BTW: Do you remember your own similar question here:

including the Howto answer:

I actually hadn't remembered that I had asked before. I have used regular expressions in the past, but I'm not understanding something about the syntax here.

I see that, according to the docs, regexps work like $regexp(x,e,r,c) where " $regexp(x,e,r,c)` replaces the pattern specified by the regular expression e in the string x by r."

It appears that I would put that in the action as
$regexp(%ALBUMARTIST%, something)

But I'm not getting how the suggested
Regular Expression:^(.+)\s(.+)$ Replace with: $2, $1
fits in there.

I have tried replacing the "something" with
$regexp(%albumartist%,^(.+)\s(.+)$)
which does nothing.

I suspect that I may have to set the "r" value to
$2, $1
but the comma in there confuses me. It appears that it if I would change it to
$regexp(%albumartist%,^(.+)\s(.+)$,$2,$1)
the $1 would be considered as the "e" value.

I'm probably missing just one bit of information as to how the syntax works, and I'm not spotting it in the docs or the community conversations. It's probably one of those things that will appear obvious to those who already know it.

In which field do you have your example
John Lennon
in the ALBUMARTIST or ARTIST?

To swap the firstname with the lastname you can use for example an Action "Replace with regular expression"

Field:
ARTIST
Regular expression:
(.*) (.*)
Replace matches with:
$2, $1

As already mentioned, this only works for ARTIST content with two parts like John Lennon. It does not work for 3 or more parts in the name.

Please use such an Action carefully and apply it only to specific tracks.

Explanation for the regular expression:
Group together all characters before the last space (=$1)
Group together all characters after the last space (=$2)
then
Replace the current content with the content of
$2 followed by a comma and a space
and the content of $1

This should explain why this regular expression does not work for example for
John Ringo Lennon (would become "Lennon, John Ringo")
or
John Ringo Starr Lennon (would become "Lennon, John Ringo Starr")

How do you get that dialog to appear? I've never seen it before. I have been editing my Actions from the Actions > Actions menu.

OK, I found that dialog (under a toolbar button that only had a graphic that I wouldn't have guessed would mean that and selecting "Replace with regular expression). Putting those values in that box worked for doing it for a single instance. Thanks.

Now back to the question that I’m trying to figure out: how would I put it into a repeatable, named action, to set the value of ALBUMARTISTSORT based on doing this to the value of ALBUMARTIST?

This is what I have so far. I still need to understand how that "$2, $1" fits in that syntax.

Oh, you are on a Mac!
Why didn't you say so?
Have a look at the documentation:

Oh! You're right, that's a significant omission on my part. I am, indeed, on a Mac.

Followup: After the welcome help here, digging through docs, and several hours of trial and error. I think I have it working.

Applying my action 'Move Files (Swap)' to an album by the ALBUMARTIST Brian Eno, with the value:

Format field "_DIRECTORY":/Volumes/PublicXL/data/media/music/$regexp(%albumartist%,^(.+)\s(.+)$,'$2, $1')/%YEAR% %ALBUM%/
resulted in the hoped-for path:

/Volumes/PublicXL/data/media/music/Eno, Brian/2017 Reflection/

The missing piece was to put the last argument to the regexp, '$2, $1', in single quotes.

Thanks, all. Hopefully, having it here will help the next person who trips over this.