Thursday, 15 March 2012

sorting - Powershell Hashtables Key Order -



sorting - Powershell Hashtables Key Order -

using powershell 2.0 there way maintain order of keys in hashtable added? push/pop mechanism.

example:

$hashtable = @{} $hashtable.add("switzerland","berne") $hashtable.add("germany","berlin") $hashtable.add("spain","madrid") $hashtable.add("italy","rome") $hashtable

i want retain order in i've added elements hashtable.

there no built in solution in powershell v1 / v2, want utilize .net system.collections.specialized.ordereddictionary

$order = new-object system.collections.specialized.ordereddictionary $order.add("switzerland","berne") $order.add("germany","berlin") ps>$order name value ---- ----- switzerland berne federal republic of germany berlin

in ps v3 can cast [ordered] :

ps>[ordered]@{"switzerland"="berne";"germany"="berlin"} name value ---- ----- switzerland berne federal republic of germany berlin

sorting powershell order hashtable powershell-v2.0

No comments:

Post a Comment