natsort: '_' can't be right sorted in Windows

In windows OS, I want to sorted files in a content with a natural way. But I found your code seems to be not well done in windows. The '_' is sorted after number '0-9'. But in windows OS, the '_' is showed before number '0-9'.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 27 (16 by maintainers)

Most upvoted comments

OK - I have finally decided how I want to implement this.

  1. I am going to introduce the functions winsorted and winsort_key into the API
  2. These functions will use the Windows API directly in order to perform their actions, similar to this answer
  3. Because the Windows API is used, this will only be available on Windows.
  4. Because the Windows API handles everything in a black-box fashion, there will be no alg option for customization of the results (though I will still provide key)

It does seem that Microsoft has a custom sorting order for characters (at least for Excel) as can be seen in the table given here. In this table the _ character appears before numbers, which is not how it appears in the ASCII table. This is why Python’s sorted (and thus natsorted) places the _ character after and not before number characters.

Having said this, it’s not clear to me what the request is here. As filed, the issue simply states that natsort is “not well done in windows” and points out the behavior of natsorted compared to Windows Explorer. Is this

  • a question on how to make natsort return the same results as Windows Explorer?
  • an enhancement request to allow the user to modify the sorting table to match Windows Explorer?

@ganego It’s out - natsort 7.1.0.

Well, my original plan was to not even export winsorted on a non-windows machine. But, after I posted my plan I realized a better solution was to instead name the function os_sorted (or something similar) and have it behave similar to your thought, where it sorts according to how the OS’s file manager would sort.