David David - 3 months ago 8
PowerShell Question

Unable to sort a 2D array in powershell

I'm getting some unique results with this little guy

clear
$Datas = @{}
$Datas["Database1"] = "440"
$Datas["Database2"] = "140"
$Datas["Database3"] = "210"
$Datas | sort value


The Results are:

Name Value
---- -----
Database3 210
Database1 440
Database2 140


The results should be:

Name Value
---- -----
Database2 140
Database3 210
Database1 440


Any idea what I am missing? Any reading material to dive into?

Answer

This blog solves your problem. In short, try this:

$Datas.GetEnumerator() | sort value

value is not necessary (sort by value by default). Use name to sort by name.