Skip to content

Instantly share code, notes, and snippets.

@michaellwest
Created May 14, 2021 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaellwest/dde65b16de0313fb6a7b26aeaf3fc598 to your computer and use it in GitHub Desktop.
Save michaellwest/dde65b16de0313fb6a7b26aeaf3fc598 to your computer and use it in GitHub Desktop.
Sort a list of IP addresses by count and IP.
$ips = @(
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"15.37.53.228"
"15.37.53.228"
"15.37.53.228"
"119.123.16.100"
"119.123.16.12"
"119.123.16.9"
"119.123.16.89"
)
$records = @()
foreach($ip in $ips) {
$records += [pscustomobject]@{
"IP" = $ip
"Version" = [String]::Join('.', ([System.Net.IPAddress]$ip).IPAddressToString.Split('.').PadLeft(3,'0'))
}
}
$records | Group-Object -Property Version |
Sort-Object -Property Count, Name -Descending |
Select-Object -Property Count, @{"Label"="IP";"Expression"={$_.Group[0].IP}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment