I've been challenged by a friend to do the following:

"Find the quickest and easiest way of sorting a directory listing by the LAST character of the filenames."

He's done it on Linux using the following:

ls | rev | sort | rev

I'd like to show him the powershell alternative, but I'm only just starting to learn powershell and I can't do it. So, I'm cheating and asking for your help.

link|improve this question

Actually that is sorting it by not just the last character but by all the characters following. Sam Cogan's answer below is strictly by the last character and ignores any following characters. – Sim Dec 4 '09 at 10:35
Before anyone votes to migrate this question again, have a look at this: meta.stackoverflow.com/questions/32471/… – raven Dec 10 '09 at 16:20
feedback

5 Answers

up vote 4 down vote accepted

Unfortunately Powershell does not have a nice easy reverse method, so instead you have to get the last letter of the string and sort by that. This is one way i've done it:

dir| sort {$_.name.Substring($_.name.length-1)}

As has been pointed out, this will sort strictly by the last letter only, whereas I the Linux version will sort by the last and then subsequent letters, so there may be a better way of doing this, or you may have to introduce some looping if you want it that way.

link|improve this answer
That's brill. Not as short as the Linux alternative, but still pretty good. – Richard Dec 4 '09 at 10:29
I noticed too that this only sorts by the last letter, unlike the linux version. However the challenge stated "by the LAST character" so this answer answers the question, even if the two solutions aren't equal. – Richard Dec 4 '09 at 10:48
Due to some confusion this question was migrated to Stack Overflow and then unmigrated. Keith Hill on Stack overflow added the shorter answer ls|sort{"$_"[-1]} – Richard Dec 9 '09 at 21:01
feedback

dir| sort {$_.name[-1]}

link|improve this answer
feedback

Shay's variant is way shorter than the accepted answer by indexing into the string but even that can be improved. You can shorten it even more by excluding unnecessary spaces and using a shorter alias:

ls|sort{$_.Name[-1]}

Also you can use the (abbreviated) -Name argument to Get-ChildItem:

ls -n|sort{$_[-1]}

which will return strings directly.

If you really want to sort by the reverse string, then the following works (but is slow):

ls -n|sort{$_[3e3..0]}

You can make it faster if you have an upper bound on the file name's length.

link|improve this answer
feedback
dir | sort -Property @{Expression ={$n = $_.Name.ToCharArray(); [Array]::Reverse($n);[String]::Join("",$n)}}


Not as short as the unix version, mostly because there isn't a String.Reverse() function in the .NET Framework. Basically this works by telling sort 'sort by computing this expression on the input arguments'.


Now, if any unix shell does better than

dir | sort -Property Length -Descending

to print all the files with the largest one first, I'd be interested to see it.

link|improve this answer
2  
How about 'ls -S'? – Bryan Dec 5 '09 at 17:21
Wow! Didn't know about this one :D Nice one :) – AntonioCS Dec 5 '09 at 21:31
feedback
ls|sort{"$_"[-1]}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.