Add Sort A-Z and Sort Z-A commands
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:
parent
9d56f5e8e2
commit
ae1d428e75
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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: ""},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue