Skip to content

feat(sort): allow sorting any column with top like navigation #2739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ventsislav-georgiev
Copy link

@ventsislav-georgiev ventsislav-georgiev commented Jun 15, 2024

Updated the PR from @mlosicki #1256
Related to: #1000

@ventsislav-georgiev
Copy link
Author

@derailed Any feedback on this? Since this is a long standing issue with a previous PR, if the proposed implementation is bad in any way, please provide some insights on how we should go on implementing it. As it is really useful to be able to sort by any column quickly.

@derailed
Copy link
Owner

@ventsislav-georgiev I think this feature is great. Let's resolve the conflict and I'll take a peek...

@derailed derailed added the needs-tlc Pr needs additional updates label Aug 16, 2024
@ventsislav-georgiev
Copy link
Author

@derailed Thanks, conflict resolved.

@ventsislav-georgiev
Copy link
Author

ping @derailed

@ventsislav-georgiev
Copy link
Author

ventsislav-georgiev commented Dec 7, 2024

@derailed Any chance we could merge this?

@derailed
Copy link
Owner

@ventsislav-georgiev Looks like we have conflicts. Can you take a peek?

@derailed derailed added enhancement New feature or request conflicts labels Feb 16, 2025
@ventsislav-georgiev ventsislav-georgiev force-pushed the sort-on-any-column branch 3 times, most recently from a3c7948 to 08d8cbc Compare February 16, 2025 20:12
@ventsislav-georgiev
Copy link
Author

@derailed rebased.

@ventsislav-georgiev
Copy link
Author

@derailed rebased again.

@ventsislav-georgiev
Copy link
Author

@derailed rebased yet again.

Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ventsislav-georgiev Thank you for this pr and rebases!

@@ -288,6 +288,26 @@ func (t *Table) SetSortCol(name string, asc bool) {
t.setSortCol(model1.SortColumn{Name: name, ASC: asc})
}

func (t *Table) GetSortCol() (string, bool) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

return t.sortCol.Name, t.sortCol.ASC
}

func (t *Table) isVisible(h model1.HeaderColumn) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor! I think this should also check for hide/wide attribute on the column.
Also we should reuse this call on buildRow below.

@@ -15,6 +15,16 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

type (
// SortChange changes the column on which to sort
SortChange bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be an int type using next, prev = +1, -1?

@@ -210,6 +210,9 @@ func (t *Table) bindKeys() {
tcell.KeyCtrlW: ui.NewKeyAction("Toggle Wide", t.toggleWideCmd, false),
ui.KeyShiftN: ui.NewKeyAction("Sort Name", t.SortColCmd(nameCol, true), false),
ui.KeyShiftA: ui.NewKeyAction("Sort Age", t.SortColCmd(ageCol, true), false),
ui.KeyQ: ui.NewKeyAction("Reverse Sort Order", t.SortInvertCmd, false),
Copy link
Owner

@derailed derailed Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Q is poor choice for the invert sort. This is on me since the keyspace is limited.
Here is a thought... What if we just use the proposed PR and eliminate the canned sort keys.
I think there are several benefits here:

  1. Frees up the key space
  2. Allows us to use the defacto R or 'I' to reverse on the selected sort column
  3. This method becomes the standard k9s sort i.e solely 3 keys aka '<|>|R' and we can reclaim everything else
    and lighten the cognitive load in the process...

Do you think that would make sense?

sortColIdx := -1
newSortColIdx := -1
prevSortColIdx := -1
header := t.GetModel().Peek().GetHeader()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal as we realize the model twice here and later in refresh which would be expensive.
So I think we should pass in the model to something like doRefresh(*model1.Data) vs calling Refresh once the sort is set.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this should call setMSort(true) to tell the table manual sorting is now in effect.

continue
}

if h.Name == sortCol {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if sortCol is not set? As it stands we would scan the header twice and yield nothing
I think it might be best to list out all visible header columns first.
Get the current sort column index if set and just go +1 or -1 based on that.
Bonus: if we are out of range we can do a circular buffer where pressing < on the first col yields the last col and > in the last col bumps you back to the first one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicts enhancement New feature or request needs-tlc Pr needs additional updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants