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 ''

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

  • 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 ''