Hashtables and key order

后端 未结 7 1057
青春惊慌失措
青春惊慌失措 2020-12-05 17:46

Is there a way to keep the order of keys in a hashtable as they were added? Like a push/pop mechanism.

Example:

$hashtable = @{}

$hashtable.Add(\"Sw         


        
7条回答
  •  时光说笑
    2020-12-05 18:04

    Here is a simple routine that works for me.

    function sortedKeys([hashtable]$ht) {
      $out = @()
      foreach($k in $ht.keys) {
        $out += $k
      }
      [Array]::sort($out)
      return ,$out
    }
    

    and the call to use it

    forEach($k in (& sortedKeys $ht)) {
      ...
    }
    

提交回复
热议问题