Sort Two Names Alphabetically

Advanced Renamer forum
#1 : 15/12-21 21:46
Ervin
Ervin
Posts: 8
Is there an easier way to swap a filename such as:

"Justin Bieber & Ariana Grande"
"Madonna & Britney Spears"

to

"Ariana Grande & Justin Bieber"
"Britney Spears & Madonna"

where it's sorted by each artist's name alphabetically.

I use swap, but then have to manually look at each filename to make sure it's sorted alphabetically by name 1 and name 2. Otherwise, it will swap filenames that are sorted correctly.


15/12-21 21:46 - edited 15/12-21 21:48
#2 : 16/12-21 14:41
David Lee
David Lee
Posts: 1071
Use a Script method with the following code:

names = item.name.match(/([^&]*?) *& *(.*)/);
if (names) {
names = names.slice(1,3).sort();
return names[0] + ' & ' + names[1];
}

Spaces before and after "&" are optional.


16/12-21 14:41
#3 : 16/12-21 19:31
Ervin
Ervin
Posts: 8
Reply to #2:
Wow ... that was quick. Thank you so much!
I don't know how to write scripts, but it works perfectly!...


16/12-21 19:31
#4 : 16/12-21 20:00
Ervin
Ervin
Posts: 8
Reply to #2:
BTW...Long shot here... but is there a script that can sort multiple names (more than two names) in a title in alphabetical order?


16/12-21 20:00
#5 : 17/12-21 09:21
David Lee
David Lee
Posts: 1071
Reply to #4:
If all your names are separated by "&" (eg "Ariana Grande & Justin Bieber & Britney Spears & Madonna"):

names = item.name.split(/ *& */).sort();
newName = names[0];
for (j=1; j<names.length; j++) newName += ' & ' + names[j];
return newName;

However if your list is in themore conventional form "Ariana Grande, Justin Bieber, Britney Spears & Madonna" use the following code:

names = item.name.split(/ *[,&] */).sort();
newName = names[0];
for (j=1; j<names.length-1; j++) newName += ', ' + names[j];
if (names.length>1) newName += ' & ' + names[j];
return newName;

In each case spaces before and after the separators are optional and filenames with only a single name will not be changed.


17/12-21 09:21
#7 : 18/12-21 12:18
David Lee
David Lee
Posts: 1071
Reply to #4:
If all your names are separated by "&" (eg "Ariana Grande & Justin Bieber & Britney Spears & Madonna"):

names = item.name.split(/ *& */).sort();
newName = names[0];
for (j=1; j<names.length; j++) newName += ' & ' + names[j];
return newName;

However if your list is in themore conventional form "Ariana Grande, Justin Bieber, Britney Spears & Madonna" use the following code:

names = item.name.split(/ *[,&] */).sort();
newName = names[0];
for (j=1; j<names.length-1; j++) newName += ', ' + names[j];
if (names.length>1) newName += ' & ' + names[j];
return newName;

In each case spaces before and after the separators are optional and filenames with only a single name will not be changed.


18/12-21 12:18
#8 : 21/12-21 20:47
Ervin
Ervin
Posts: 8
Reply to #5:
Wow... Thanks David!


21/12-21 20:47