Sort IPv4 and IPv6 Addresses Correctly

by Mar 14, 2018

When you try and sort IPv4 addresses via Sort-Object, this fails:

 
PS> '10.1.2.3', '2.3.4.5', '1.2.3.4' | Sort-Object
1.2.3.4
10.1.2.3
2.3.4.5 

This is no surprise because the data is of type “string”, so Sort-Object uses alphanumeric sorting. In the previous tip we showed how you can cast the data to [Version] and pretend you are sorting software versions:

 
PS> '10.1.2.3', '2.3.4.5', '1.2.3.4' | Sort-Object -Property { $_ -as [Version] }
1.2.3.4
2.3.4.5
10.1.2.3

However, this fails with IPv6 addresses because they cannot be turned into a version number:

 
PS> '10.1.2.3', 'fe80::532:c4e:c409:b987%13', '2.3.4.5', '2DAB:FFFF:0000:3EAE:01AA:00FF:DD72:2C4A', '1.2.3.4' | Sort-Object -Property { $_ -as [Version] }

fe80::532:c4e:c409:b987%13
2DAB:FFFF:0000:3EAE:01AA:00FF:DD72:2C4A
1.2.3.4
2.3.4.5
10.1.2.3

Converting IPv6 addresses to [Version] using the operator -as yields NULL, which is why IPv6 addresses appear at the top of the list in exactly the order they were fed into Sort-Object.

To sort IPv6 addresses, the default alphanumerical sorting would be adequate, so let’s just differentiate, and when an IPv6 address is encountered, use its string value for sorting:

$code = {
    $version = $_ -as [Version]
    if ($version -eq $null) { "z$_" }
    else { $version }
} 

'10.1.2.3', 'fe80::532:c4e:c409:b987%13', '2.3.4.5', '2DAB:FFFF:0000:3EAE:01AA:00FF:DD72:2C4A', '1.2.3.4' | Sort-Object -Property $code

Now the result looks nice and clean:

 
10.1.2.3
2.3.4.5
1.2.3.4
2DAB:FFFF:0000:3EAE:01AA:00FF:DD72:2C4A
fe80::532:c4e:c409:b987%13

Note how the code adds a “z” to the string representation of IPv6 addresses. This ensures that IPv6 addresses will appear at the bottom of the list. If you’d like them to be at the top of the list, try this:

$code = {
    $version = $_ -as [Version]
    if ($version -eq $null) { "/$_" }
    else { $version }
}

Since “/” has a lower ASCII value than “0”, now IPv6 addresses appear at the top of the list:

 
2DAB:FFFF:0000:3EAE:01AA:00FF:DD72:2C4A
fe80::532:c4e:c409:b987%13
1.2.3.4
2.3.4.5
10.1.2.3 

Do you know PowerShell Conference EU 2018, taking place April 17-20 in Hanover, Germany? If you are an advanced PowerShell professional, you shouldn’t miss this year’s agenda: www.psconf.eu: Hover over a session to view its abstract.

With 45 international top speakers including PowerShell inventor Jeffrey Snover, 80 sessions, and workshops, this event is much more than just a conference. It is a one-of-a-kind Advanced PowerShell Training and a very unique opportunity to meet friendly PowerShell gurus, get authoritative answers to even the trickiest PowerShell questions, and bring home fresh ideas.

This conference is not profit-driven, and all speakers volunteer. The delegate fee basically covers venue, food and drinks throughout the conference, evening events with grand dinner, and workshops.

Just don’t wait too long: this unique event is limited to 300 delegates, with 265 seats already taken by the time of this writing. Visit http://www.powershellmagazine.com/2018/02/09/powershell-conference-eu-2018/ for more details, or www.powershell.love for a quick impression of last year.

Twitter This Tip! ReTweet this Tip!