Plugin: Sort lines

name

Sort lines

icon

sort_by_alpha

description

Sorts selected lines using natural sort and also allows reversing order

instructions

This plugin will sort selected lines. It uses a sorting algorithm according to your browser locale for correct lexigraphical order and also tries to sort numerical prefixes naturally, i.e. 1, 2, ... 9, 10, 11, 12 and not 1, 10, 12, 2, 3, ... 9 even when there are no leading zerores.


"Sort lines: Ascending" will sort the lines from A to Z / smallest to largest

"Sort lines: Descending" will sort the lines from Z to A / largest to smallest

"Sort lines: Reverse existing" will simply reverse the order of the lines, without sorting them

Note: Due to limitations in the Amplenote plugin system, formatting cannot be retained!

☕ If you like my work, you can buy me a coffee!

({
_collator: new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base'
}),
 
replaceText: {
'Ascending': {
async run (app, text) {
const lines = text.split('\n').map(x => x.trim()).filter(x => x)
const sortedLines = lines.sort((a, b) => this._collator.compare(a, b))
await app.context.replaceSelection(sortedLines.join('\n\n'))
}
},
'Descending': {
async run (app, text) {
const lines = text.split('\n').map(x => x.trim()).filter(x => x)
const sortedLines = lines.sort((a, b) => this._collator.compare(a, b))
await app.context.replaceSelection(sortedLines.reverse().join('\n\n'))
}
},
'Reverse existing': {
async run (app, text) {
const lines = text.split('\n').map(x => x.trim()).filter(x => x)
await app.context.replaceSelection(lines.reverse().join('\n\n'))
}
}
}
})