# Button to rename files, sorting components of their names

**URL:** https://resource.dopus.com/t/button-to-rename-files-sorting-components-of-their-names/34298
**Category:** Help & Support
**Tags:** rename
**Created:** [January 12, 2020, 2:55pm UTC](https://resource.dopus.com/t/button-to-rename-files-sorting-components-of-their-names/34298 "2020-01-12T14:55:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Georg73](https://resource.dopus.com/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@Georg73](https://resource.dopus.com/u/Georg73)
#### Post date: [January 12, 2020, 2:55pm UTC](https://resource.dopus.com/t/button-to-rename-files-sorting-components-of-their-names/34298/1 "2020-01-12T14:55:23Z")

</div>

I need a button to rename files like ...

Tony, Carl, Andy - 2002.txt  
--\> Andy, Carl, Tony - 2002.txt  
or  
New York, Dublin, Atlanta, Berlin, London - George.txt  
--\> Atlanta, Berlin, Dublin, London, New York - George.txt

Alphabetical sort of the filename part before the "-"

Thanks a lot ...

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [January 12, 2020, 9:37pm UTC](https://resource.dopus.com/t/button-to-rename-files-sorting-components-of-their-names/34298/2 "2020-01-12T21:37:16Z")

</div>

Here's a Rename Preset which does that:

- [SortNameParts.orp](https://resource.dopus.com/uploads/short-url/4QnF8A0FQmfDfulHcbZd73wVkxl.orp) (793 Bytes)
- [How to use Rename Presets from this forum](https://resource.dopus.com/t/how-to-use-rename-presets-from-this-forum/26726)

For reference:

 ![image](https://resource.dopus.com/uploads/default/original/3X/2/8/285cc3f4d1dc19ba7124ac99f98f52a00e3f3ba6.png)

```javascript
function OnGetNewName(getNewNameData)
{
	// Separate name and extension.
	var stem = getNewNameData.item.name_stem;
	var ext = getNewNameData.item.ext;

	// Separate anything after the last " - ".
	var suffix = "";
	var suffixPos = stem.lastIndexOf(" - ");
	if (suffixPos != -1)
	{
		suffix = stem.substr(suffixPos);
		stem = stem.substr(0, suffixPos);
	}

	// Sort components between ", "
	stem = stem.split(", ").sort().join(", ");

	// Put everything back together
	return stem + suffix + ext;
}

```

---

<div class="post-metadata">

### Author: ![Georg73](https://resource.dopus.com/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@Georg73](https://resource.dopus.com/u/Georg73)
#### Post date: [January 13, 2020, 4:03pm UTC](https://resource.dopus.com/t/button-to-rename-files-sorting-components-of-their-names/34298/3 "2020-01-13T16:03:01Z")

</div>

Wow, I am your Fan!!! Please send me your Autograph Card 😜
