Sort-Object and Integers
Import-CSV
imports csv columns as strings
by default. You need to cast it to int
before you can sort by value, and not "alphabetical". Ex:
$csv = Import-Csv -Header "Color", "Number" -delimiter ';' data.csv
$csv | % { $_.Number = [int]$_.Number }
$csv | Sort-Object Number
Color Number
----- ------
Orange 65
Purple 85
White 110
Green 140
Red 160
As an alternative, you can cast it while sorting, like this:
$csv = Import-Csv -Header "Color", "Number" -delimiter ';' data.csv
$csv | Sort-Object @{e={$_.Number -as [int]}}
or even shorter $csv | Sort-Object { [int]$_.Number }