# Wednesday, February 24, 2010

PowerShell guru and Admin-extraordinare Jeff Hicks asked a great question that many a Windows administrator has probably asked themselves when working with Hashtables in PowerShell: Why do the key/values come out in a different order than put in? This is a great question and the answer lies in computer science theory, particularly computational complexity theory. Rather than bore you with a ton of nonsense about O(1), O(n) and other propeller-head dribble, I figure I could explain it in terms everyone should be able to understand.

Hashtables, Buckets and HashCodes = Rolodex, Index Cards and Surnamescard-index

Yes, if the light hasn’t gone on yet, it will soon. Every .NET object includes a method called GetHashCode. This method returns a number that represents the identity of the object in a kind of fuzzy way. I say “fuzzy” because the hash code for a given instance of an object can be different on different platforms (xp, vista, 2003 etc) or on different bitness (64 vs 32 bit.) This method is used by hashtable to get the “surname” of an object. Instead of Index Cards, a Hashtable uses “buckets” to separate groups of objects. Any given Hashcode will naturally fall into a particular bucket as the function (result) of a high-speed optimized algorithm, much like any given surname naturally falls under a particular letter of the alphabet. Finally, it should be clear to you that using index cards [Hashtable buckets] is way faster than flicking through an unsorted folder [randomized list.]

Just like a Rolodex, the order you add names to it doesn’t dictate the order they are in as you flip through the cards sequentially, which is analogous to sending a Hashtable to out-default.

posted on Wednesday, February 24, 2010 5:38:55 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] Trackback
Related posts:
PowerShell Script Provider
PowerShell ISE Hacking: Change default save encoding to ASCII
PowerShell 2.0 – PSCX Labs: Invoke-Reflector
PowerShell 2.0 – Developer Essentials #1 – Initializing a Runspace with a Module
SharePoint Resources & Localization – What, Where and Why?
PowerShell 2.0 – Partial Application of Functions and Cmdlets

Referred by:
http://www.google.com/ig [Referral]
http://mail.google.com/mail/?ui=2&view=bsp&ver=1qygpcgurkovy [Referral]
http://twitter.com/ [Referral]
http://untiny.me/ [Referral]
http://bl147w.blu147.mail.live.com/mail/InboxLight.aspx?Fold... [Referral]
http://www.google.com/reader/view/ [Referral]
.net hashtable moss get by guid (www.google.fi) [Referral]
PowerShell (www.google.co.jp) [Referral]
http://live.feedjit.com/news/CN/wenzu/ [Referral]
http://powershellgroup.org/content/more-features-powershellg... [Referral]
http://www.google.nl/webhp?hl=en#num=30&hl=en&newwindow=1&q=... [Referral]
http://www.google.nl/ig?hl=nl&source=iglk [Referral]
powershell hashtables of objects (www.google.co.uk) [Referral]
http://bit.ly/cB5P1t [Referral]
http://jdhitsolutions.com/blog/ [Referral]
http://powershell.com/cs/forums/p/3590/4943.aspx#4943 [Referral]
http://www.netvibes.com/jailbreakiphone3gs [Referral]
http://powershell.com/cs/forums/t/3590.aspx [Referral]
powershell ordered hashtable (www.google.fr) [Referral]
winrm using hash tables (cc.bingj.com) [Referral]
http://twitter.com//jeffhicks [Referral]
http://www.google.com/ [Referral]
bl147w.blu147.mail.live.com (www.google.ro) [Referral]
powerscript hashtable (www.google.be) [Referral]
powershell hashtable (www.google.com) [Referral]
powershell sorted hashtable (www.google.com) [Referral]
powershell using sorted hashtable (www.google.com) [Referral]
http://github.com/zelnorm [Referral]
http://github.com/aggrenox [Referral]
bl147w blu147 mail live default (www.google.fr) [Referral]
powershell hashtable keys (www.google.co.uk) [Referral]
powershell hashtable ordered (www.google.com) [Referral]
powershell hash tables (www.google.com) [Referral]
http://ig.gmodules.com/gadgets/ifr?view=home&url=http://1o4.... [Referral]
powershell hashtable (www.google.com) [Referral]
powershell hash table keys (www.google.nl) [Referral]
powershell 2.0 hash tables (www.google.com) [Referral]
powershell hashtable order (www.google.co.uk) [Referral]
Powershell hash tables (www.google.com) [Referral]
powershell hash table out of order (www.google.co.uk) [Referral]
powershell .keys (www.google.com) [Referral]
are keys sorted in hashtable in asp.net (www.google.co.in) [Referral]
powerscript hashtable (www.google.co.nz) [Referral]
hashtable powershell (www.google.ca) [Referral]
powershell hashtable of hashtables (www.google.co.za) [Referral]
powershell hashtables (www.google.co.uk) [Referral]
powershell check for key in hashtable (www.google.com) [Referral]
powershell hashtable ordering (www.google.com) [Referral]
http://www.netvibes.com/zetia [Referral]
Comments are closed.