earnestt1234 / seedir

A Python package for creating, editing, and reading folder tree diagrams
MIT License
122 stars 9 forks source link

Sorting capital letters before lowercase ones? #14

Closed Ondrej-Sulc closed 2 years ago

Ondrej-Sulc commented 2 years ago

Hi, I ran into some sorting issues while using your library today.

While using "sort=True", It sorted the folder "COMPONENTS" before "Compensators" even though it should be the other way around. I fixed it by changing the folder name to "Components". I am guessing it's sorting the uppercase letters before the lowercase ones and it does not matter what's behind them. Is this intended behavior?

earnestt1234 commented 2 years ago

@Ondrej-Sulc Thanks for the comment! This is intended behavior, in so much as this is the typical behavior of sorting in Python:

>>> sorted(['Zuchini', 'banana', 'apple'])
['Zuchini', 'apple', 'banana']

seedir actually uses natsort, rather than the built-in sorted , but the ranking of capital/lowercase letters does not change. The intended way to get your desired output is to use sort_key when calling seedir. I.e.:

seedir.seedir('/your/path', sort_key = lambda x: x.lower())

It's the same idea as doing case-insensitive sorting with sorted.