Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

You can't sort strings in ASCII order

When you pass chars to Sort-Object, the chars sorted in ASCII order.

PS > (0x7E..0x21 | ForEach-Object { [char]$_ } | Sort-Object) -join ''
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

But when you pass strings to Sort-Object, the strings are not sorted in ASCII order, but according to Unicode collation algorithm. c.f. http://unicode.org/charts/collation/

  • Punctuation characters are first
  • Numbers are in the between punctuation characters and alphabets
  • Alphabets are last
    • Order of uppercase and lowercase is not stable
PS > (0x7E..0x21 | ForEach-Object { "{0}" -F [char]$_ } | Sort-Object) -join ''
_-,;:!?.'"()[]{}@*/\&#%`^+<=>|~$0123456789AaBbcCdDeEfFgGhHIiJjKkLlMmNnOopPQqRrsStTuUvVWwXxyYZz