List排序问题

演绎精彩人生_ 2013-06-26 03:37:45
现有一个需求,需要对List<string>里面的Ip进行排序,
例如,集合是这样的:
 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.26.184
115.238.26.185
115.238.26.186
115.236.102.216
172.16.88.2
172.16.88.3
172......
192.168.1.1
192.168.1.7
192....
222.73.29.77
222...
请问大家这个如何实现?
...全文
545 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 16 楼 lyc0884 的回复:
[quote=引用 12 楼 sp1234 的回复:] [quote=引用 7 楼 yumen3501 的回复:] linq 哦
学会linq,你就不需要自定义那么多方法了。[/quote]请问如果是一个 List<TbServers>对象了[/quote] 那就根据对象的某个属性,如人的身高,年龄都是排序,楼上几位都已经分析的很清楚了。
  • 打赏
  • 举报
回复
可以放到数组里面,数组有自动的排序功能。
  • 打赏
  • 举报
回复
引用 12 楼 sp1234 的回复:
[quote=引用 7 楼 yumen3501 的回复:] linq 哦
学会linq,你就不需要自定义那么多方法了。[/quote]请问如果是一个 List<TbServers>对象了
  • 打赏
  • 举报
回复
谢谢大家的回帖,轻微如果是 List<TbServers>对象了?
饕餮123 2013-06-26
  • 打赏
  • 举报
回复
楼主的+号,好纠结
  • 打赏
  • 举报
回复
引用 7 楼 yumen3501 的回复:
linq 哦
学会linq,你就不需要自定义那么多方法了。
  • 打赏
  • 举报
回复
如果觉得嵌套的表达式看着麻烦,可以写成比较傻瓜化的
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();
        }

    }

}
x_jefferson 2013-06-26
  • 打赏
  • 举报
回复
嗯,楼上说的都很有理
  • 打赏
  • 举报
回复
你可以把只有一位或者两位显示的字节补齐为三位的,例如 172.16.88.9 补齐之后就是 172.016.088.009。然后再用这个结果作为key来为原始ip集合排序。
  • 打赏
  • 举报
回复
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();
        }

    }

}
Assassin_ 2013-06-26
  • 打赏
  • 举报
回复
linq 哦
q107770540 2013-06-26
  • 打赏
  • 举报
回复
OJ的方法是可以的,在LINQ里同样也可以实现:
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]));
		}
	}
EnForGrass 2013-06-26
  • 打赏
  • 举报
回复
IpList = IpList.OrderBy(x=>x.Substring(0,x.IndexOf('.'))).ToList();
EnForGrass 2013-06-26
  • 打赏
  • 举报
回复
IpList=IpList.OrderBy(ip => ip).ToList();
我姓区不姓区 2013-06-26
  • 打赏
  • 举报
回复

            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);

q107770540 2013-06-26
  • 打赏
  • 举报
回复
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
		*/
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧