我有以下列表:
var ips = new List<string> {
"192.168.5.1",
"192.168.0.2",
"192.168.0.3",
"192.168.0.4",
"192.168.1.1",
"192.168.1.2",
"192.168.1.3",
"192.168.1.4"
}.OrderBy(p => p.Ip);
看起来它有效,是否有必要编写自定义比较器,如this one:
public class MyComparer : IComparer<string>
{
public int Compare(string x, string y)
{
int ip1 = IPAddress.Parse(x).ToInteger();
int ip2 = IPAddress.Parse(y).ToInteger();
return (((ip1 - ip2) >> 0x1F) | (int)((uint)(-(ip1 - ip2)) >> 0x1F));
}
}