Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

'os_sorted' sorts files with spaces in names not as in Windows Explorer #165

Closed
Tomarchelone opened this issue May 29, 2023 · 2 comments
Closed
Labels

Comments

@Tomarchelone
Copy link

The bug
I have two files with space in one name, say 'a.pdf' and 'a b.pdf'. os_sorted sorts these names as
'a.pdf'
'a b.pdf'

Expected behavior
Windows Explorer sorts the files in the following order:

'a b.pdf'
'a.pdf'

I expect the os_sorted funcion to do the same.

Environment:

  • Python Version: 3.11.1
  • Windows

To Reproduce

from natsort import os_sorted

print(os_sorted(['a.pdf', 'a b.pdf']))

If it is of any help, the code from here seems to sort the files as in Windows Explorer. Namely:

from ctypes import wintypes, windll
from functools import cmp_to_key

def winsort(data):
    _StrCmpLogicalW = windll.Shlwapi.StrCmpLogicalW
    _StrCmpLogicalW.argtypes = [wintypes.LPWSTR, wintypes.LPWSTR]
    _StrCmpLogicalW.restype  = wintypes.INT

    cmp_fnc = lambda psz1, psz2: _StrCmpLogicalW(psz1, psz2)
    return sorted(data, key=cmp_to_key(cmp_fnc))


@SethMMorton
Copy link
Owner

SethMMorton commented Jun 20, 2023

This is a result of code that attempts to sort e.g. "file.txt" before "file (2).txt". This is how it sorts on my Linux machine, and if I remember correctly on Mac as well. I just checked, and Windows sorts this as "file (2).txt" before "file.txt", so this treatment is incorrect for Window. I will fix it.

@SethMMorton
Copy link
Owner

Closed with #166

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jun 28, 2023
[8.4.0] - 2023-06-19
---

### Changed

- The changelog now only explictly exists in the repository
  (issue [#162](SethMMorton/natsort#162))

### Fixed

- Inputs with spaces near adjascent to the extension are now sorted
  correctly for `os_sorted` on Windows (issues
  [#165](SethMMorton/natsort#165) and
  [#166](SethMMorton/natsort#166))
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants