diff --git a/app_test.go b/app_test.go index c157820..788df1e 100644 --- a/app_test.go +++ b/app_test.go @@ -98,7 +98,7 @@ func TestParseCSVString(t *testing.T) { func TestGetCommands(t *testing.T) { reg := NewCommandRegistry() cmds := reg.GetCommands() - if len(cmds) != 12 { + if len(cmds) != 14 { t.Errorf("expected 12 commands, got %d", len(cmds)) } // Check that all have IDs diff --git a/commands.go b/commands.go index 32609ef..467a54e 100644 --- a/commands.go +++ b/commands.go @@ -30,5 +30,7 @@ func (c *CommandRegistry) GetCommands() []Command { {ID: "open-down", Name: "Insert Row Below", Shortcut: ""}, {ID: "open-left", Name: "Insert Column Left", 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: ""}, } } diff --git a/frontend/src/main.js b/frontend/src/main.js index a717c81..e9a1e02 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -607,6 +607,8 @@ async function loadCommands() { { ID: 'open-down', Name: 'Insert Row Below', Shortcut: '' }, { ID: 'open-left', Name: 'Insert Column Left', 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-left': doInsertColLeft(); 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'); } +// ===== 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 ===== async function loadFile(filePath) { try {