I'm sure someone can do this better, but here is one way that is fully compatible with lynix. It has the benefit of leaving you with a reusable rev
string function for your toolbox, i.e. it sorts the entire string and not just the last character:
function rev ($s) {return -join ($s[$s.Length..0])}
dir | foreach{rev($_.name)} | sort | foreach{rev($_)}
I think the foreach's here nicely demonstrate how PowerShell pipes are arrays and not simply strings like in *nix.
It took me a little while to realize that I had to use only $_
and not $_.name
inside the 2nd foreach
. So I've learned something about variations in the array content from one pipe to the next.
*Credit for the guts of my rev function goes to http://rosettacode.org/wiki/Reverse_a_string#PowerShell
Works like lynix:
- dir | sort -Property @{Expression ={$n = $_.Name.ToCharArray(); [Array]::Reverse($n);[String]::Join("",$n)}}
Sort of works like lynix, but very, very slow:
Do not work like lynix, i.e. fail to sort all characters of the file names; (only sorts the very last character of the string):
- dir| sort {$.name.Substring($.name.length-1)}
- dir| sort {$_.name[-1]}
- ls|sort{$_.Name[-1]}
- ls|sort{"$_"[-1]}
- ls -n|sort{$_[-1]}