I'm trying to create a script that will list the servers I need to list along with the latency returned from each server, I need the list to be in ascending or descending order based on ping.

What I've come up with so far has given basically what I want, but if the ping returned is above 100 on one server, the other values are considered to be higher if they start with a value higher than 1.

I understand that I need the ping value returned as an integer, but I haven't been able to figure out exactly how to accomplish that with out removing the server name.

Anyway, here's what I've come up with so far

```
function TestNetwork {
    $global:array = @("<ServerName>:<ipaddress>"; "<ServerName>:<ipaddress>"; "<ServerName>:<ipaddress>")

    foreach ($str in $global:array) {
        $avg = 0
        $server = $($str -split ":")[0]
        $PingServer = Test-Connection -Count 3 $server
        $avg = ($PingServer | Measure-Object ResponseTime -Average)
        $calc = [System.Math]::Round($avg.Average)
        $output = "{000} | " -f $($calc) + $server
        $Output
    }
}
TestNetwork | Sort numerical -Descending `

```

and here are the results I get

```

75 | Server    
73 | Server  
110 | Server  

```
['https://Stackoverflow.com/questions/50032109', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9631948/']
There are several ways to address your problem.

You could [format](http://social.technet.microsoft.com/wiki/contents/articles/7855.using-the-f-format-operator-in-powershell.aspx) the number in a way that aligns integer sort order and string sort order as [LotPings suggested](https://stackoverflow.com/questions/50032109/how-do-i-sort-a-string-by-its-numerical-value-in-powershell#comment87081424_50032109):

```
'{0,3} | {1}' -f $calc, $server

```

You could also extract the number from the string, convert it back to an integer, and use that as the sorting property:

```
... | Sort-Object {[int]($_ -replace '(\d+).*', '$1')} -Descending

```

However, I wouldn't advise either approach. The PoSh way of doing things is to work with objects as [Olaf pointed out](https://stackoverflow.com/questions/50032109/how-do-i-sort-a-string-by-its-numerical-value-in-powershell#comment87080444_50032109), so wrap your data in custom objects and convert them to strings only if you have a reason to do so (e.g. to create text output):

```
foreach ($str in $global:array) {
    ...
    New-Object -Type PSObject -Property ([ordered]@{
        'ComputerName' = $server
        'ResponseTime' = $calc
    })
}

```

That way you can "naturally" sort your data:

```
TestNetwork | Sort ResponseTime -Descending | ForEach-Object {
    '{0,3} | {1}' -f $_.ResponseTime, $_.ComputerName
}

```

or

```
TestNetwork | Sort ResponseTime -Descending | Export-Csv 'output.csv' -NoType

```
Cast string to integer

```
TestNetwork | Sort {[int]($_.numerical)} -Descending

```