Comments (4)
I am very reluctant to have natsort
do any automatic modification of the algorithm settings based on the input data because I don't want to remove control from the user. Also, scanning the input data and doing type checking like this for a less common use case would undo most of the optimizations I have added in the most recent releases.
I am wondering, is there a reason that you aren't sure the type of the incoming data? If you know that you want to sort paths, you can just always add the ns.P
modifier since non-path strings will still get sorted correctly, it will just eat up some runtime.
Would the following be acceptable alternatives?
>>> # Convert to string if the input is a Path object.
>>> natsorted(mylist, key=lambda x: str(x) if isinstance(x, pathlib.Path) else x, alg=ns.P)
or
>>> # Just convert everything to strings.
>>> natsorted(mylist, key=lambda x: str(x), alg=ns.P)
I might be open to adding an option that will do the checking and turning on of the path algorithm, but the user has to request this explicitly.
from natsort.
Oh, of course I know the type of the data and key argument works. At the end it's all about practicality vs. speed. What about using the first key argument (convert only Path instances to str) when ns.P is on?
from natsort.
I will look into this.
from natsort.
I have fixed this in a recent commit. I will release as an incremental update (version 3.5.2) in a few days.
from natsort.
Related Issues (20)
- Can't use natsort_keygen() as key for sorting DataFrame with MultiIndex in pandas HOT 3
- Some values don't sort in a consistent order HOT 3
- Set which OS to sort by in `os_sorted` HOT 8
- Paths should be sorted like strings HOT 6
- Loosen types and type checking
- Sorting a list of dictionaries when the sort field might or might not have a number HOT 2
- Improve os_sorted performance by avoiding `Path` roundtrips HOT 1
- add a mode for hexadecimal numbers HOT 7
- 1 test fails HOT 5
- RFE: drop use `m2r2` module HOT 8
- Sorting income category with both string and num HOT 1
- compatibility with GNU coreutils sort -n (numeric sort) HOT 14
- 'os_sorted' sorts files with spaces in names not as in Windows Explorer HOT 2
- not consistent with behavior of windows chinese edition HOT 1
- `cmp_to_key` gives an error and I don't know if this is a bug or just not supported HOT 3
- wiki pages so huge that they are not loaded properly HOT 2
- Error while sorting dates if `NaT` HOT 3
- Unexpected natural sort when sorting multi-dimensional arrays or `pandas.DataFrame` HOT 14
- Support Python 3.12 HOT 2
- Character based sorting HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from natsort.