PowerShell 3.0 CTP1 introduces a new feature [ordered] which is somewhat a shortcut for OrderedDictionary. I cannot imagine practical use cases of it. Why is this feature really useful? Can somebody provide some useful examples?
Example: this is, IMHO, rather demo case than practical:
$a = [ordered]@{a=1;b=2;d=3;c=4}
(I do not mind if it is still useful, then I am just looking for other useful cases).
I am not looking for use cases of OrderedDictionary, it is useful, indeed. But we can use it directly in v2.0 (and I do a lot). I am trying to understand why is this new feature [ordered] needed in addition.
Collected use cases from answers:
$hash = [ordered]@{}
is shorter than
$hash = New-Object System.Collections.Specialized.OrderedDictionary
N.B. ordered is not a real shortcut for the type. New-Object ordered does not work.
N.B. 2: But this is still a good shortcut because (I think, cannot try) it creates typical for PowerShell case insensitive dictionary. The equivalent command in v2.0 is too long, indeed:
New-Object System.Collections.Specialized.OrderedDictionary]([System.StringComparer]::OrdinalIgnoreCase)
First I will start by asking – Why not have them?
I can think of a use case in my project where we use Powershell scripts for build and deploy and yml for config ( using https://github.com/scottmuc/PowerYaml )
The config from yml is read in as hashtables. The tasks are specified in the yml like deploy to database, deploy to iis, deploy service and so on. I would like to do the database deploy and then website deploy so that I can avoid iisreset afterwards. Currently I have to explicitly look at that. Now, I can have an Ordered hash table and specify the deploy database first and hence it happens first.
Snippet:
Now the
$hash =@{}become$hash=[ordered]@{}I don’t know how this means for a bloated product. OrderedDictionary is there in .NET ( I have lots of use cases for it in programming ) and they have just added an accelerator for it.