Richard's profileRichard Siddaway's BlogPhotosBlogLists Tools Help

Blog


    July 19

    Sorting a hash table

    There is a really good PowerShell script for working with indexed files - scans or photos and renumbering to cope with missing numbers available from http://www.tellingmachine.com/post/2008/07/Renaming-a-series-of-indexed-files-with-Powershell.aspx

    One point at the end of the post is about sorting hash tables.  This is not intuitive as piping the hash table into sort does not work.  You need to use the GetEnumerator() before trying to sort like this

    PS> $a = @{}
    PS> $a[[int]1] = "dir scan0001.txt"
    PS> $a[[int]11] = "dir scan0011.txt"
    PS> $a[[int]3] = "dir scan0003.txt"
    PS> $a

    Name                           Value
    ----                           -----
    3                              dir scan0003.txt
    1                              dir scan0001.txt
    11                             dir scan0011.txt

    PS> $a.getEnumerator() | Sort Key -Descending

    Name                           Value
    ----                           -----
    11                             dir scan0011.txt
    3                              dir scan0003.txt
    1                              dir scan0001.txt

    PS> $a.getEnumerator() | Sort Name -Descending

    Name                           Value
    ----                           -----
    11                             dir scan0011.txt
    3                              dir scan0003.txt
    1                              dir scan0001.txt

    Create a hash table as shown.  Use the getEnumerator() method and pipe into sort.  Sorted!

     

    Share this post :

     

    Technorati Tags:

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Picture of Anonymous
    Klaus Graefensteiner wrote:
    Thanks Richard,
     
    the sorting works like a charm.
     
    Klaus
    July 20

    Trackbacks

    The trackback URL for this entry is:
    http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1551.trak
    Weblogs that reference this entry
    • None