62,046
社区成员
发帖
与我相关
我的任务
分享
List<string> IpList = new List<string>() {
"172.16.88.220"
+"222.73.29.77"
+"192.168.1.8"
+"172.16.88.2"
+"192.168.1.7"
+"172.16.88.10"
+"172.16.88.3"
+"172.16.88.8"
+"172.16.88.225"
+"172.16.88.100"
+"221.130.187.180"
+"221.130.187.181"
+"222.73.55.168"
+"172.16.88.7"
+"222.73.29.79"
+"172.16.88.200"
+"172.16.88.125"
+"192.168.1.1"
+"222.73.55.250"
+"115.236.102.216"
+"172.17.22.161"
+"222.73.29.135"
+"222.73.29.136"
+"172.17.22.162"
+"172.16.88.9"
+"115.238.26.184"
+"115.238.26.185"
+"115.238.26.186"
};
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> IpList = new List<string>() {
"172.16.88.220"
,"222.73.29.77"
,"192.168.1.8"
,"172.16.88.2"
,"192.168.1.7"
,"172.16.88.10"
,"172.16.88.3"
,"172.16.88.8"
,"172.16.88.225"
,"172.16.88.100"
,"221.130.187.180"
,"221.130.187.181"
,"222.73.55.168"
,"172.16.88.7"
,"222.73.29.79"
,"172.16.88.200"
,"172.16.88.125"
,"192.168.1.1"
,"222.73.55.250"
,"115.236.102.216"
,"172.17.22.161"
,"222.73.29.135"
,"222.73.29.136"
,"172.17.22.162"
,"172.16.88.9"
,"115.238.26.184"
,"115.238.26.185"
,"115.238.26.186"};
var result = from x in IpList
let 分解各个字节 = x.Split('.')
let 补齐字节到3位 = from y in 分解各个字节 select string.Format("{0:C3}", y)
let 补齐后的ip = string.Join(".", 补齐字节到3位.ToArray())
orderby 补齐后的ip
select x;
foreach (var x in result)
Console.WriteLine(x);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> IpList = new List<string>() {
"172.16.88.220"
,"222.73.29.77"
,"192.168.1.8"
,"172.16.88.2"
,"192.168.1.7"
,"172.16.88.10"
,"172.16.88.3"
,"172.16.88.8"
,"172.16.88.225"
,"172.16.88.100"
,"221.130.187.180"
,"221.130.187.181"
,"222.73.55.168"
,"172.16.88.7"
,"222.73.29.79"
,"172.16.88.200"
,"172.16.88.125"
,"192.168.1.1"
,"222.73.55.250"
,"115.236.102.216"
,"172.17.22.161"
,"222.73.29.135"
,"222.73.29.136"
,"172.17.22.162"
,"172.16.88.9"
,"115.238.26.184"
,"115.238.26.185"
,"115.238.26.186"};
var result = from x in IpList
let 补齐字符串 = string.Join(".", x.Split('.').Select(y => string.Format("{0:C3}", y)).ToArray())
orderby 补齐字符串
select x;
foreach (var x in result)
Console.WriteLine(x);
Console.ReadKey();
}
}
}
void Main()
{
List<string> IpList = new List<string>() {
"172.16.88.220"
,"222.73.29.77"
,"192.168.1.8"
,"172.16.88.2"
,"192.168.1.7"
,"172.16.88.10"
,"172.16.88.3"
,"172.16.88.8"
,"172.16.88.225"
,"172.16.88.100"
,"221.130.187.180"
,"221.130.187.181"
,"222.73.55.168"
,"172.16.88.7"
,"222.73.29.79"
,"172.16.88.200"
,"172.16.88.125"
,"192.168.1.1"
,"222.73.55.250"
,"115.236.102.216"
,"172.17.22.161"
,"222.73.29.135"
,"222.73.29.136"
,"172.17.22.162"
,"172.16.88.9"
,"115.238.26.184"
,"115.238.26.185"
,"115.238.26.186"
};
IpList=IpList.OrderBy(i=>i,new MyCompare()).ToList();
IpList.ForEach(i=>Console.WriteLine(i));
}
public class MyCompare : IComparer<string>
{
public int Compare(string stringA, string stringB)
{
string[] array1 = stringA.Split('.');
string[] array2 = stringB.Split('.');
int index = 0;
bool equal = true;
while (equal && index < 4)
{
equal = array1[index] == array2[index];
index++;
}
return int.Parse(array1[index - 1]).CompareTo(int.Parse(array2[index - 1]));
}
}
List<string> IpList = new List<string>() {
"172.16.88.220"
,"222.73.29.77"
,"192.168.1.8"
,"172.16.88.2"
,"192.168.1.7"
,"172.16.88.10"
,"172.16.88.3"
,"172.16.88.8"
,"172.16.88.225"
,"172.16.88.100"
,"221.130.187.180"
,"221.130.187.181"
,"222.73.55.168"
,"172.16.88.7"
,"222.73.29.79"
,"172.16.88.200"
,"172.16.88.125"
,"192.168.1.1"
,"222.73.55.250"
,"115.236.102.216"
,"172.17.22.161"
,"222.73.29.135"
,"222.73.29.136"
,"172.17.22.162"
,"172.16.88.9"
,"115.238.26.184"
,"115.238.26.185"
,"115.238.26.186"
,"115.238.102.216"
};
IpList.Sort((s1, s2) =>
{
string[] array1 = s1.Split('.');
string[] array2 = s2.Split('.');
int index = 0;
bool equal = true;
while (equal && index < 4)
{
equal = array1[index] == array2[index];
index++;
}
return int.Parse(array1[index - 1]).CompareTo(int.Parse(array2[index - 1]));
});
foreach (string ip in IpList)
Console.WriteLine(ip);
void Main()
{
List<string> IpList = new List<string>() {
"172.16.88.220"
,"222.73.29.77"
,"192.168.1.8"
,"172.16.88.2"
,"192.168.1.7"
,"172.16.88.10"
,"172.16.88.3"
,"172.16.88.8"
,"172.16.88.225"
,"172.16.88.100"
,"221.130.187.180"
,"221.130.187.181"
,"222.73.55.168"
,"172.16.88.7"
,"222.73.29.79"
,"172.16.88.200"
,"172.16.88.125"
,"192.168.1.1"
,"222.73.55.250"
,"115.236.102.216"
,"172.17.22.161"
,"222.73.29.135"
,"222.73.29.136"
,"172.17.22.162"
,"172.16.88.9"
,"115.238.26.184"
,"115.238.26.185"
,"115.238.26.186"
};
IpList=IpList.OrderBy(i=>i).ToList();
IpList.ForEach(i=>Console.WriteLine(i));
/*
115.236.102.216
115.238.26.184
115.238.26.185
115.238.26.186
172.16.88.10
172.16.88.100
172.16.88.125
172.16.88.2
172.16.88.200
172.16.88.220
172.16.88.225
172.16.88.3
172.16.88.7
172.16.88.8
172.16.88.9
172.17.22.161
172.17.22.162
192.168.1.1
192.168.1.7
192.168.1.8
221.130.187.180
221.130.187.181
222.73.29.135
222.73.29.136
222.73.29.77
222.73.29.79
222.73.55.168
222.73.55.250
*/
}