Skip to content
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

Rows sorting? #218

Open
majki09 opened this issue Mar 27, 2024 · 3 comments
Open

Rows sorting? #218

majki09 opened this issue Mar 27, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@majki09
Copy link

majki09 commented Mar 27, 2024

How do I sort rows with some column?

@ragardner
Copy link
Owner

Hello,

There is currently no in-built way to do this, it would require getting the table and index data which is a list of lists, the sublists are rows, sorting this and then setting the table and index data to the newly sorted data

Once you have the data from the table a basic way of sorting the list of lists by a column would be something like this:
https://stackoverflow.com/a/4174955/7655687

There are more advanced methods which take into account digits utilising the sort() methods key= parameter

For example:

def sort():
    col = 1
    sort_key = lambda row: [
            int(c) if c.isdigit() else c.lower() for c in re.split("([0-9]+)", f"{row[col]}")
        ]
    span = sheet[:].options(index=True)
    sheet.set_data(span, data=sorted(span.data, key=sort_key))

You will have to research what key you want to use as I am not experienced with sorting, natural sorting etc.

There are some libraries which handle it pretty well, for example: https://github.com/SethMMorton/natsort

I hope this helps!

@majki09
Copy link
Author

majki09 commented Apr 10, 2024

Is it that simple? Thank you so much for the answer and all your effort for tksheet 😀

@majki09 majki09 closed this as completed Apr 10, 2024
@TweetleDee916
Copy link

Would love to have this as a function available to the sheet itself. at least a simple lexicographic sort on one column for example would be easy to implement. #238

@ragardner ragardner reopened this Feb 6, 2025
@ragardner ragardner added the enhancement New feature or request label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants