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

Feature request natsorted(): Sort by letters, then numbers #48

Closed
NtwkEngineer opened this issue Feb 7, 2018 · 3 comments · Fixed by #49
Closed

Feature request natsorted(): Sort by letters, then numbers #48

NtwkEngineer opened this issue Feb 7, 2018 · 3 comments · Fixed by #49
Labels
Milestone

Comments

@NtwkEngineer
Copy link

NtwkEngineer commented Feb 7, 2018

I'd like to submit a feature request to add an option that would allow natsorted() to sort by letters first, then numbers instead of numbers first, then letters. Here is an example of the list that I am looking to sort - I am sorting by index 2 ('ABCDE', 'BCDEF' and '10001'):

from natsort import natsorted
a = [
	['192.168.1.1', 'generic_description_1', 'ABCDE', 'apples', '172.16.1.1', u'\nCircuit is up\n'],
	['192.168.1.1', 'generic_description_2', 'BCDEF', 'oranges', '172.16.2.1', u'\nCircuit is up\n'],
	['192.168.2.1', 'generic_description_3', '10001', 'bananas', '172.16.3.1', u'\nCircuit is up\n']
]
print(natsorted(a, key = lambda x: x[2]))

Here are the results that are printed:

[['192.168.2.1', 'generic_description_3', '10001', 'bananas', '172.16.3.1', u'\nCircuit is up\n'],
 ['192.168.1.1', 'generic_description_1', 'ABCDE', 'apples', '172.16.1.1', u'\nCircuit is up\n'],
 ['192.168.1.1', 'generic_description_2', 'BCDEF', 'oranges', '172.16.2.1', u'\nCircuit is up\n']]

And as you can see, it sorts by index 2 by numbers first, then letters:

10001
ABCDE
BCDEF

The option I am requesting would allow the sorting by letters first, then numbers - like this:

ABCDE
BCDEF
10001

Thanks for the consideration!

-Matt

@SethMMorton
Copy link
Owner

SethMMorton commented Feb 7, 2018

This is a good request. The only reason natsort currently puts numbers first is that it was the easier thing to do and I made the assumption that that is what users would want. The process of maintaining this library has taught me that either my assumptions are always wrong, or not to make assumptions.

@SethMMorton
Copy link
Owner

Can you test out the code in pull request #49 and respond indicating whether or not it addresses your request?

@NtwkEngineer
Copy link
Author

Hi Seth!

Works great with my production data. Thanks so much for the fast implementation of this request! :)

-Matt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants