{"id":36024,"date":"2007-07-09T19:01:49","date_gmt":"2007-07-09T19:01:49","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/andrewarnottms\/2007\/07\/09\/how-to-sort-the-console-output-of-a-hashtable-in-powershell\/"},"modified":"2019-04-03T22:50:10","modified_gmt":"2019-04-04T05:50:10","slug":"how-to-sort-the-console-output-of-a-hashtable-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/how-to-sort-the-console-output-of-a-hashtable-in-powershell\/","title":{"rendered":"How to sort the console output of a hashtable in PowerShell"},"content":{"rendered":"<p>Hashtables are inherently unsorted, but when you&#8217;re printing&nbsp;a hashtable&#8217;s contents to the console, it can certainly be helpful to sort its contents by key.&nbsp; Although it&#8217;s not obvious, the way to do it is pretty easy.<\/p>\n<p>Let&#8217;s start with defining a hashtable and play with it until it&#8217;s obviously unsorted.<\/p>\n<pre class=\"command\">PS C:\\&gt; $h = @{b=2;a=3;c=1}\r\nPS C:\\&gt; $h\r\n\r\nName                           Value\r\n----                           -----\r\na                              3\r\nb                              2\r\nc                              1\r\n\r\n\r\nPS C:\\&gt; $h['d']=5\r\nPS C:\\&gt; $h\r\n\r\nName                           Value\r\n----                           -----\r\na                              3\r\nb                              2\r\nd                              5\r\nc                              1\r\n<\/pre>\n<p>Great. Now let&#8217;s sort it:<\/p>\n<pre class=\"command\">PS C:\\&gt; $h | sort name\r\n\r\nName                           Value\r\n----                           -----\r\na                              3\r\nb                              2\r\nd                              5\r\nc                              1\r\n<\/pre>\n<p>Ergh! What happened? Well, without going into tons of detail, it has to do with the type that backs HashTable and how you can&#8217;t sort the object itself. You&#8217;ve got to generate a list of key-value pairs and sort that.<\/p>\n<p>Here&#8217;s the correct way to do it:<\/p>\n<pre class=\"command\">PS C:\\&gt; $h.GetEnumerator() | sort name\r\n\r\nName                           Value\r\n----                           -----\r\na                              3\r\nb                              2\r\nc                              1\r\nd                              5\r\n<\/pre>\n<p>Great. And yes, sorting by value works too:<\/p>\n<pre class=\"command\">PS C:\\&gt; $h.GetEnumerator() | sort value\r\n\r\nName                           Value\r\n----                           -----\r\nc                              1\r\nb                              2\r\na                              3\r\nd                              5\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hashtables are inherently unsorted, but when you&#8217;re printing&nbsp;a hashtable&#8217;s contents to the console, it can certainly be helpful to sort its contents by key.&nbsp; Although it&#8217;s not obvious, the way to do it is pretty easy. Let&#8217;s start with defining a hashtable and play with it until it&#8217;s obviously unsorted. PS C:\\&gt; $h = @{b=2;a=3;c=1} [&hellip;]<\/p>\n","protected":false},"author":2685,"featured_media":37840,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[4617,102],"class_list":["post-36024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-permierdev","tag-andarno","tag-powershell"],"acf":[],"blog_post_summary":"<p>Hashtables are inherently unsorted, but when you&#8217;re printing&nbsp;a hashtable&#8217;s contents to the console, it can certainly be helpful to sort its contents by key.&nbsp; Although it&#8217;s not obvious, the way to do it is pretty easy. Let&#8217;s start with defining a hashtable and play with it until it&#8217;s obviously unsorted. PS C:\\&gt; $h = @{b=2;a=3;c=1} [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/36024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/2685"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=36024"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/36024\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=36024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=36024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=36024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}