Right now I have this Google Spreadsheet code I pieced together, which does 2 types of sorting:
- By character count, then alphabetically.
- By syllable count, then alphabetically.
Here is the code:
const VOWEL_PATTERN = /[ieaou]/i;
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu("Sort")
.addItem("Sort by length then alphabetically", "sortByLength")
.addItem("Sort by syllables then alphabetically", "sortBySyllable")
.addToUi();
}
function sortBySyllable() {
const range = SpreadsheetApp.getActive().getDataRange();
const array = range.getValues();
const sortedArray = [array[0]].concat(
array.slice(1).sort((a, b) => {
const xp = a[0];
const yp = b[0];
return (
xp.split(VOWEL_PATTERN).length - yp.split(VOWEL_PATTERN).length ||
xp.length - yp.length ||
xp.localeCompare(yp)
);
})
);
range.setValues(sortedArray);
}
function sortByLength() {
const range = SpreadsheetApp.getActive().getDataRange();
const array = range.getValues();
const sortedArray = [array[0]].concat(
array.slice(1).sort((a, b) => {
const xp = a[0];
const yp = b[0];
return xp.length - yp.length || xp.localeCompare(yp);
})
);
range.setValues(sortedArray);
}
That works fine, given that it sorts according to the standard unicode sorting algorithm (I guess?).
However, I am working on a fantasy language, and in my spreadsheet I want to sort the letters in a particular order. Let's say this is the order I want to sort them in:
const ALPHABETICAL_ORDER = 'ieaoumnqgdbptkhsfvzjxcCwylr'
How do I then somewhat efficiently sort the string by this custom alphabetical order?