我有一个IP地址列表如下
192.168.1.5
69.52.220.44
10.152.16.23
192.168.3.10
192.168.1.4
192.168.2.1
我正在寻找这种方法来对此列表进行排序以匹配以下顺序
10.152.16.23
69.52.220.44
192.168.1.4
192.168.1.5
192.168.2.1
答案 0 :(得分:46)
这可能看起来像一个黑客,但它完全符合你的需要:
var unsortedIps =
new[]
{
"192.168.1.4",
"192.168.1.5",
"192.168.2.1",
"10.152.16.23",
"69.52.220.44"
};
var sortedIps = unsortedIps
.Select(Version.Parse)
.OrderBy(arg => arg)
.Select(arg => arg.ToString())
.ToList();
答案 1 :(得分:10)
您可以将每个IP地址转换为如此的整数...
69.52.220.44 =>
69 * 255 * 255 * 255 +
52 * 255 * 255 +
220 * 255 +
44
然后按整数表示排序。
答案 2 :(得分:1)
您可能会发现此功能也很有用。
public static class ExtensionMethods
{
public static int CompareTo(this IPAddress x, IPAddress y)
{
var result = x.AddressFamily.CompareTo(y.AddressFamily);
if (result != 0)
return result;
var xBytes = x.GetAddressBytes();
var yBytes = y.GetAddressBytes();
var octets = Math.Min(xBytes.Length, yBytes.Length);
for (var i = 0; i < octets; i++)
{
var octetResult = xBytes[i].CompareTo(yBytes[i]);
if (octetResult != 0)
return octetResult;
}
return 0;
}
}
答案 3 :(得分:0)
您可以将Array.Sort函数与我们将创建的用于比较两个IP的函数一起使用:
//ips is string array
Array.Sort(ips, IpCompare);
然后将此函数放入代码中。
private static int IpCompare(string x, string y)
{
string ip1 = x + '.', ip2 = y + '.';
string xSection = "", ySection = "";
for (int i = 0; i < ip1.Length && i < ip2.Length; i++)
{
if (ip1[i] == '.' && ip2[i] == '.')
{
if (xSection != ySection)
return int.Parse(xSection) - int.Parse(ySection);
xSection = ""; // Start compare the next section
ySection = "";
}
else if (ip1[i] == '.') return -1; //The first section is smaller because it's length is smaller
else if (ip2[i] == '.') return 1;
else
{
xSection += ip1[i];
ySection += ip2[i];
}
}
return 0;
//If we would find any difference between any section it would already return something.
//so that mean that both IPs are the same
}
答案 4 :(得分:0)
简单的解决方案不需要黑客。只需对地址段进行Split(),用零填充这些段,然后将它们重新连接在一起。将这一行静态方法放入这样的静态类中:
public static class StringHelper
{
public static string IpAddressLabel(string ipAddress)
=> string.Join(".", ipAddress.Split('.').Select(part => part.PadLeft(3, '0')));
}
然后随意调用:
=> new[] {"192.168.1.100", "192.168.1.1", "192.168.1.19"}
.OrderBy(ip => StringHelper.IpAddressLabel(ip));
此外,当需要可排序的标签时,它可以用作文件名或其他位置:
192.168.001.001.log
192.168.001.019.log
192.168.001.100.log