Sorting a Hastable by Key

%3CLINGO-SUB%20id%3D%22lingo-sub-1205204%22%20slang%3D%22en-US%22%3ESorting%20a%20Hastable%20by%20Key%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1205204%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20together%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20try%20to%20sort%20a%20hastable%20by%20key(s)%20but%20have%20no%20luck.%3C%2FP%3E%3CP%3EAbout%20my%20hastable%2C%20the%20key%20is%20a%20string%20and%20the%20value%20is%20a%20psobject%20of%20pair%20of%20data.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20what%20i%20dit%20to%20sort%20the%20hastbale%20is%20somthing%20like%20this%3A%3C%2FP%3E%3CP%3E%24myHashTable%20%3D%26nbsp%3B%40%7B%7D%3C%2FP%3E%3CP%3E....%3C%2FP%3E%3CP%3E%23%20fill%20the%20hashtable%20with%20my%20objects%3C%2FP%3E%3CP%3E%24myHashTable%20.Add(%221%22%2C%20%24MyPsObject)%3C%2FP%3E%3CP%3E%24myHashTable%20.Add(%225%22%2C%20%24MyPsObject)%3C%2FP%3E%3CP%3E%24myHashTable%20.Add(%223%22%2C%20%24MyPsObject)%3C%2FP%3E%3CP%3E%24myHashTable%20.Add(%222%22%2C%20%24MyPsObject)%3C%2FP%3E%3CP%3E%24myHashTable%20.Add(%224%22%2C%20%24MyPsObject)%3C%2FP%3E%3CP%3E....%3C%2FP%3E%3CP%3E%23%20Sort%20the%20HashTable%3C%2FP%3E%3CP%3E%24myHashTable%20%3D%26nbsp%3B%24myHashTable.getEnumerator()%20%7C%20Sort-Object%20-property%20key%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20soon%20as%20it%20is%20sortet%2C%20it%20is%20not%20longer%20a%20hashtable%20with%20all%20the%20members%20etc.%20After%20the%20sort%20the%20variable%20is%20a%20DictionaryEntry%20and%20i%20haven't%20any%20of%20the%20hashtable%20members%20i%20need.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20can%20you%20please%20help%20me%20to%20sort%20my%20hashtable%20by%20key%20and%20after%20the%20sorting%20i%20can%20use%20the%20members%20for%20hashtables%20as%20well%20with%20it%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%2C%3C%2FP%3E%3CP%3ENico%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1205204%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1205804%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20a%20Hastable%20by%20Key%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1205804%22%20slang%3D%22en-US%22%3E%3CP%3ESimply%20create%20it%20as%20an%20ordered%20one%3F%20Although%20it's%20technically%20%22%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EOrderedDictionary%3C%2FFONT%3E%22%20and%20not%20%22hashtable%22%2C%20it%20works%20pretty%20much%20the%20same.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1205911%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20a%20Hastable%20by%20Key%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1205911%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3Bis%20right%2C%20there's%20actually%20no%20way%20to%20guarantee%20the%20order%20of%20a%20hashtable.%20I%20come%20up%20against%20this%20all%20the%20time%20when%20using%20them%20to%20create%20PSCustomObjects%20and%20wanting%20to%20control%20the%20order%20of%20the%20properties.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20you'll%20want%20to%20use%20an%20ordered%20dictionary%2C%20in%20most%20cases%20you%20won't%20notice%20a%20difference%20(and%20if%20you%20do%20I'd%20love%20to%20hear%20about%20the%20use%20case!)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20jump%20straight%20to%20creating%20an%20ordered%20dictionary%2C%20add%20an%20%5Bordered%5D%20when%20you're%20initiating%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%24myHashTable%20%3D%20%5Bordered%5D%20%40%7B%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20can't%20control%20the%20order%20in%20which%20you're%20adding%20items%20you%20can%20sort%20a%20hashtable%20into%20an%20ordered%20dictionary%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%23%20Standard%20hashtable%0A%0A%24myHashTable%20%3D%20%40%7B%7D%0A%0A%23%20Fill%20with%20random%20ordered%20data%0A%0A%24myHashTable.Add(%221%22%2C%20'Test1')%0A%24myHashTable.Add(%225%22%2C%20'Test5')%0A%24myHashTable.Add(%223%22%2C%20'Test3')%0A%24myHashTable.Add(%222%22%2C%20'Test2')%0A%24myHashTable.Add(%224%22%2C%20'Test4')%0A%0A%23%20Create%20an%20ordered%20dictionary%0A%0A%24orderedDictionary%20%3D%20%5Bordered%5D%20%40%7B%7D%0A%0A%23%20Sort%20the%20keys%2C%20add%20key%20and%20ke's%20value%20to%20dictionary%0A%0Aforeach%20(%24Key%20in%20(%24myHashTable.Keys%20%7C%20Sort-Object))%20%7B%0A%20%20%20%20%24orderedDictionary.Add(%24Key%2C%20%24myHashTable.%24Key)%20%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1213258%22%20slang%3D%22en-US%22%3ERe%3A%20Sorting%20a%20Hastable%20by%20Key%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1213258%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F62408%22%20target%3D%22_blank%22%3E%40Joshua%20King%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20of%20all%20thank%20you%20for%20aour%20answer%20and%20your%20sample.%3C%2FP%3E%3CP%3EI%20will%20try%20to%20change%20my%20code%20and%20hope%20it%20will%20work.%20I%20let%20you%20know%20the%20outcome.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ebr%2C%3C%2FP%3E%3CP%3ENico%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello together,

 

I try to sort a hastable by key(s) but have no luck.

About my hastable, the key is a string and the value is a psobject of pair of data.

 

So what i dit to sort the hastbale is somthing like this:

$myHashTable = @{}

....

# fill the hashtable with my objects

$myHashTable .Add("1", $MyPsObject)

$myHashTable .Add("5", $MyPsObject)

$myHashTable .Add("3", $MyPsObject)

$myHashTable .Add("2", $MyPsObject)

$myHashTable .Add("4", $MyPsObject)

....

# Sort the HashTable

$myHashTable = $myHashTable.getEnumerator() | Sort-Object -property key

 

As soon as it is sortet, it is not longer a hashtable with all the members etc. After the sort the variable is a DictionaryEntry and i haven't any of the hashtable members i need.

 

So can you please help me to sort my hashtable by key and after the sorting i can use the members for hashtables as well with it?

 

Thank you,

Nico

 

3 Replies

Simply create it as an ordered one? Although it's technically "OrderedDictionary" and not "hashtable", it works pretty much the same.

@Vasil Michev is right, there's actually no way to guarantee the order of a hashtable. I come up against this all the time when using them to create PSCustomObjects and wanting to control the order of the properties.

 

Instead you'll want to use an ordered dictionary, in most cases you won't notice a difference (and if you do I'd love to hear about the use case!)

 

If you want to jump straight to creating an ordered dictionary, add an [ordered] when you're initiating it:

 

$myHashTable = [ordered] @{}

 

If you can't control the order in which you're adding items you can sort a hashtable into an ordered dictionary:

 

# Standard hashtable

$myHashTable = @{}

# Fill with random ordered data

$myHashTable.Add("1", 'Test1')
$myHashTable.Add("5", 'Test5')
$myHashTable.Add("3", 'Test3')
$myHashTable.Add("2", 'Test2')
$myHashTable.Add("4", 'Test4')

# Create an ordered dictionary

$orderedDictionary = [ordered] @{}

# Sort the keys, add key and ke's value to dictionary

foreach ($Key in ($myHashTable.Keys | Sort-Object)) {
    $orderedDictionary.Add($Key, $myHashTable.$Key) 
}

@Joshua King 

First of all thank you for aour answer and your sample.

I will try to change my code and hope it will work. I let you know the outcome.

 

br,

Nico