Altering the alpha sort behavior

This seems like a basic question, so I did my due diligence to search the forums before posting. Using macOS 14.7.5 and Drafts Pro for Mac 48.2.1

I need to sort a list of titles alphabetically. But the built in sort action script does not ignore the word “A” or “The” or “An”, etc.

Eg: a list of titles of books, sorted the proper way [manually]:

The Burning Hills
The Californios
The Daybreakers
Dutchman’s Flat
The Empty Land
The Ferguson Rifle
The First Fast Draw
Flint
Galloway
The Lonely Men
The Man Called Noon
The Mountain Valley War
Sackett’s Land
The Shadow Riders
Somewhere They Die
Treasure Mountain
Utah Blaine
Valley of the Sun
The Walking Drum
War Party

and using the Sort Action:

Dutchman’s Flat
Flint
Galloway
Sackett’s Land
Somewhere They Die
The Burning Hills
The Californios
The Daybreakers
The Empty Land
The Ferguson Rifle
The First Fast Draw
The Lonely Men
The Man Called Noon
The Mountain Valley War
The Shadow Riders
The Walking Drum
Treasure Mountain
Utah Blaine
Valley of the Sun
War Party

Using sed I can do some manipulation but I am unsure how to get the results I am looking for editing the script for this action [the end result is I want a list like the first one that needs no further tweaking].

Thanks!

This bit of untested javascript should help or, at least, point you in the right direction.

const titles = [
“The Lord of the Rings”,
“A Game of Thrones”,
“An Ember in the Ashes”,
“The Catcher in the Rye”,
“Moby Dick”,
“The Hobbit”
];

function sortIgnoringArticles(list) {
return list.sort((a, b) => {
const strip = title => title.replace(/^(the|a|an)\s+/i, “”);
return strip(a).localeCompare(strip(b));
});
}

const sorted = sortIgnoringArticles(titles);
console.log(sorted);

1 Like

Thank you! I will do some testing and if I get it working, I will update this thread and make the action available to others. :slight_smile:

A quick update to this. After doing more research, and using the correct search on the Actions page, I found someone already did the work: Sort lines (ignore case and articles) | Drafts Directory

1 Like