Sort action puts large caps at the beginning of list

Hello,
There is an existing sort action https://actions.getdrafts.com/a/1LX with the choice of ascending/descending.
The action puts large caps in sorted order at the beginning of the list, followed by small caps in sorted order, which is interesting in terms of a different way of conceiving lists
Is there a way to modify the script so that the action ignores caps, ie sorts the list without taking caps into account. Or is there a simpler way of sorting with Drafts ?
thank you


// sort

(() => {

const hasSelection = () => editor.getSelectedRange()[1] > 0;

const getText = () => {
  const e = editor;
  return hasSelection() ? 
    e.getTextInRange(...e.getSelectedLineRange()) :
    e.getText();
};

const setText = (t) => {
  const e = editor;
  if (hasSelection()) {
    const sel = e.getSelectedLineRange();
    e.setTextInRange(...sel, t);
    e.setSelectedRange(sel[0], t.length);
  } else {
    e.setText(t);
  }
};

let l = getText().split('\n');

// remove blank lines
l = l.filter(l => l.trim());

if (unique) {
    l = l.filter((v,i,a) => a.indexOf(v) === i);
}

l.sort();
if (desc) {
    l.reverse();
}

setText(l.join('\n') + '\n');

})();

Uppercase characters and lowercase characters are different characters and have to be distinguished in some way. I use something similar to this for case-insensitive sorting, hopefully the following will do what you want.

let lineRange = editor.getSelectedLineRange();
let astrLines = editor.getTextInRange(lineRange[0], lineRange[1]).split('\n');
astrLines.sort((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'}));
editor.setTextInRange(lineRange[0], lineRange[1], astrLines.join('\n'));

For reversing it, you could add some code to reverse order the array before writing it back into the editor. It’s easy to lookup how to reverse an array with an online search.

1 Like

Or of course you could just modify line 3 slightly to switch the order of the parameters :man_facepalming:

astrLines.sort((a, b) => b.localeCompare(a, undefined, {sensitivity: 'base'}));
1 Like

I rushed to implement your changes and forgot to thank you for the scripts.