Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Add natural sorting function to cell content #1271

Closed
aborruso opened this issue Jan 16, 2022 · 4 comments
Closed

Add natural sorting function to cell content #1271

aborruso opened this issue Jan 16, 2022 · 4 comments
Labels

Comments

@aborruso
Copy link
Contributor

Hi,
today I needed to sort (natural sorting) this list of values 15,1,2/AX,22,1/C,1/2,1/1,1/A,1/BA,2,3, to obtain 1,1/1,1/2,1/A,1/BA,1/C,2,2/AX,3,15,22.

I did this using the shell

echo "15,1,2/AX,22,1/C,1/2,1/1,1/A,1/BA,2,3" | tr , "\n" | sort -V | paste -sd, -

I'm not able to build a plugin, but I have build a Python hello world example, and some of the skilled users, could turn it into a plugin or a VisiData function:

import re

_nsre = re.compile('([0-9]+)')
def natural_sort_key(s):
    return [int(text) if text.isdigit() else text.lower()
            for text in re.split(_nsre, s)]

mStr = '15,1,2/AX,22,1/C,1/2,1/1,1/A,1/BA,2,3'
list1 = mStr.split(',')

list1.sort(key=natural_sort_key)

joined_string = ",".join(list1)

The result is 1,1/1,1/2,1/A,1/BA,1/C,2,2/AX,3,15,22.

The list separator should become an option.

@aborruso
Copy link
Contributor Author

I'm adding two really interesting related URLs:

Using natsort cli, the "locale" option (a sort of human and non ASCII sorting):

echo "SNC-1-10-1r-1/A-2/10-10/1A2-10/A-1000/C1-a" | tr - "\n" | natsort -l  | paste -sd- -

gives me

1-1/A-1r-2/10-10-10/1A2-10/A-1000/C1-a-SNC

@frosencrantz
Copy link
Contributor

I had wanted to respond to this for some time.

It is pretty easy to add your function into your .visidatarc and then use
it with addcol-expr to create a new column with the sorted value, then type in natsort(curcol) or natsort(curcol, delimiter='/'):

import re

_nsre = re.compile('([0-9]+)')
def natural_sort_key(s):
    return [int(text) if text.isdigit() else text.lower()
            for text in re.split(_nsre, s)]

def natsort(value, delimiter=','):
   list1 = value.split(delimiter)
   list1.sort(key=natural_sort_key)
   joined_string = delimiter.join(list1)
   return joined_string

I've wanted to do something similar with a column of values. It looks like it is possible to create a custom column type, and then use https://github.com/SethMMorton/natsort/ to sort the values. If the values were wrapped in a class, it looks like it would be possible to do something like this for adding natural sort order: SethMMorton/natsort#60 (comment)

Also, this seems like more a request for a new plugin from the community than from Saul or Anja directly. I am wondering if we could add another issue tag for plugin requests or community volunteers.

@aborruso
Copy link
Contributor Author

aborruso commented Feb 6, 2022

Thank you very much @frosencrantz

@saulpw
Copy link
Owner

saulpw commented Feb 13, 2022

Yeah, I agree with @frosencrantz, for the time being I think this should be more of a plugin, and we can see how it evolves. Converting to a discussion, feel free to continue to toss around ideas.

Repository owner locked and limited conversation to collaborators Feb 13, 2022
@saulpw saulpw converted this issue into discussion #1287 Feb 13, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

3 participants