0

I have a directory of files that I needed to sort (case-sensitive). I invoked the command, but it is not giving the correct output. I tried ("BjFuHnTV.txt","bALAahas.txt") | Sort -CaseSensitive as a test, and it is outputting bALAahas.txt BjFuHnTV.txt, which is clearly false. The correct output should be BjFuHnTV.txt bALAahas.txt. Using the -Descending flag would not solve my problem either because I am trying to sort the strings in lexicogrophically ascending order.

Sorting the same two strings in Python gives the opposite result, and I am not sure why PowerShell is sorting these two strings incorrectly, even with the appropriate flags.

I am using PowerShell 7.0.6 (LTS) on Windows 10.

EDIT: B has a lower ordinal value than b in ASCII (66 vs 98 respectively), so it should obviously come first in the sorting. Bash was sorting this correctly, but PowerShell is not working as expected.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
Krish
  • 415
  • 2
  • 11
  • (a) What platform (Windows? Linux?) and PowerShell version (6? 7?) are you using? (b) Are you absolutely certain that `Sort` is aliased to `Sort-Object` rather than calling an external command (e.g., Windows `sort.exe` or a similar Linux command)? – Jeff Zeitlin Jul 12 '21 at 14:42
  • I am using Windows 10. I have tried both Sort and Sort-Object, yet there has been no difference. – Krish Jul 12 '21 at 14:47
  • Why is it "clearly false"? Because `b` has a higher ordinal value than `B` in ASCII, or...? – Mathias R. Jessen Jul 12 '21 at 14:48
  • @MathiasR.Jessen Exactly, the lowercase b has a higher ordinal value than the uppercase B in ASCII, so the file name with the uppercase B would come first in the sorting. I said that I wanted to sort lexicographically ascending. – Krish Jul 12 '21 at 14:53
  • Some experimentation seems suggestive that - for whatever reason - PowerShell 7 considers case-sensitive sorts to be lowercase-before-uppercase. I can't find any confirmation at learn.microsoft.com, however. – Jeff Zeitlin Jul 12 '21 at 15:00
  • You might find http://blog.matticus.net/2014/09/a-look-at-how-powershell-handles-case.html relevant. – Jeff Zeitlin Jul 12 '21 at 15:02

0 Answers0