Add Sort A-Z and Sort Z-A commands
Some checks failed
Build / build-linux (push) Failing after 13s
Build / build-linux-webkit2_41 (push) Failing after 14s
Build / build-macos (push) Successful in 3m44s

Sorts all rows by the currently selected column using
locale-aware alphanumeric comparison (numeric: true).

Co-authored-by: Shelley <shelley@exe.dev>
This commit is contained in:
exe.dev user 2026-03-04 22:21:49 +00:00
parent 9d56f5e8e2
commit ae1d428e75
3 changed files with 20 additions and 1 deletions

View file

@ -98,7 +98,7 @@ func TestParseCSVString(t *testing.T) {
func TestGetCommands(t *testing.T) { func TestGetCommands(t *testing.T) {
reg := NewCommandRegistry() reg := NewCommandRegistry()
cmds := reg.GetCommands() cmds := reg.GetCommands()
if len(cmds) != 12 { if len(cmds) != 14 {
t.Errorf("expected 12 commands, got %d", len(cmds)) t.Errorf("expected 12 commands, got %d", len(cmds))
} }
// Check that all have IDs // Check that all have IDs

View file

@ -30,5 +30,7 @@ func (c *CommandRegistry) GetCommands() []Command {
{ID: "open-down", Name: "Insert Row Below", Shortcut: ""}, {ID: "open-down", Name: "Insert Row Below", Shortcut: ""},
{ID: "open-left", Name: "Insert Column Left", Shortcut: ""}, {ID: "open-left", Name: "Insert Column Left", Shortcut: ""},
{ID: "open-right", Name: "Insert Column Right", Shortcut: ""}, {ID: "open-right", Name: "Insert Column Right", Shortcut: ""},
{ID: "sort-asc", Name: "Sort A-Z", Shortcut: ""},
{ID: "sort-desc", Name: "Sort Z-A", Shortcut: ""},
} }
} }

View file

@ -607,6 +607,8 @@ async function loadCommands() {
{ ID: 'open-down', Name: 'Insert Row Below', Shortcut: '' }, { ID: 'open-down', Name: 'Insert Row Below', Shortcut: '' },
{ ID: 'open-left', Name: 'Insert Column Left', Shortcut: '' }, { ID: 'open-left', Name: 'Insert Column Left', Shortcut: '' },
{ ID: 'open-right', Name: 'Insert Column Right', Shortcut: '' }, { ID: 'open-right', Name: 'Insert Column Right', Shortcut: '' },
{ ID: 'sort-asc', Name: 'Sort A-Z', Shortcut: '' },
{ ID: 'sort-desc', Name: 'Sort Z-A', Shortcut: '' },
]; ];
} }
} }
@ -706,6 +708,8 @@ async function executeCommand(id) {
case 'open-down': doInsertRowBelow(); break; case 'open-down': doInsertRowBelow(); break;
case 'open-left': doInsertColLeft(); break; case 'open-left': doInsertColLeft(); break;
case 'open-right': doInsertColRight(); break; case 'open-right': doInsertColRight(); break;
case 'sort-asc': doSort(true); break;
case 'sort-desc': doSort(false); break;
} }
} }
@ -919,6 +923,19 @@ function doInsertColRight() {
setStatus('Inserted column right'); setStatus('Inserted column right');
} }
// ===== Sorting =====
function doSort(ascending) {
const col = state.cursor.col;
state.rows.sort((a, b) => {
const va = (a[col] || '').toLowerCase();
const vb = (b[col] || '').toLowerCase();
return ascending ? va.localeCompare(vb, undefined, { numeric: true })
: vb.localeCompare(va, undefined, { numeric: true });
});
render();
setStatus(`Sorted by ${state.headers[col] || colLabel(col)} ${ascending ? 'A-Z' : 'Z-A'}`);
}
// ===== File loading ===== // ===== File loading =====
async function loadFile(filePath) { async function loadFile(filePath) {
try { try {