【问题标题】:Sort ListView by IP按 IP 排序 ListView
【发布时间】:2016-08-15 21:51:00
【问题描述】:

我有一个包含数据类型的 ListView

class InfoItem { public string IP { get; set; } public string MAC { get; set; } public string HOST { get; set; } }

事件处理程序 PingCompletedCallback 以随机方式获取 IP,因此我们无法预测 Ips 的顺序。我们需要对它们进行排序。我用这个

if (!Dispatcher.CheckAccess())
            {

                Dispatcher.Invoke(new Action(() =>
                {
                    lstNetworks.Items.Add(new InfoItem() { IP = e.Reply.Address.ToString(), MAC = macAdress, HOST = hostName });
                    lstNetworks.Items.SortDescriptions.Add(new SortDescription("IP", ListSortDirection.Ascending));
                }));
            }

它部分有效,但结果看起来像这样

192.168.1.1 192.168.1.10 192.168.1.2 192.168.1.254 192.168.1.3 等等……

我们怎样才能以正确的方式对这个 ListView 项目进行排序

192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.10 192.168.1.254

更新。我试图在那个问题中这样做:

List<InfoItem> list = new List<InfoItem>(); 
foreach (var item in lstNetworks.Items) { 
list.Add(item as InfoItem); 
} 
List<InfoItem> list2 = new List<InfoItem>(); 
list2 = list.Select(Version.Parse).OrderBy(arg => arg).Select(arg => arg.ToString()).ToList(); 

但它给了我和异常方法 Select 的类型参数不能从用法中推断出来。

【问题讨论】:

  • 您需要为 IP 地址创建自定义排序。
  • @Evan Trimboli 对 ips 数组进行排序不是问题,我需要对其进行排序,而不是松散 IP MAC 和主机名之间的依赖关系。所以我需要对所有列表进行排序
  • 完全一样的问题。您需要按 ip 对InfoItem 的列表进行排序。列表是可排序的。
  • 我该怎么做:List list = new List(); foreach(lstNetworks.Items 中的 var item){ list.Add(item as InfoItem); } List list2 = new List(); list2 = list.Select(Version.Parse).OrderBy(arg => arg).Select(arg => arg.ToString()).ToList();给我和异常无法从用法中推断方法 Select 的类型参数。

标签: c# sorting


【解决方案1】:

正如另一个答案中提到的,您可以使用Version.Parse 来做到这一点:

public class Thing {
    public string ip;
}

var list = new List<Thing>() {
    new Thing() { ip = "192.168.1.1" },
    new Thing() { ip = "192.168.1.10" },
    new Thing() { ip = "192.168.1.2" },
    new Thing() { ip = "192.168.1.254" },
    new Thing() { ip = "192.168.1.3" }
};
var sorted = list.OrderBy(item => Version.Parse(item.ip));
foreach (var item in sorted) {
    Console.WriteLine(item.ip);
}

【讨论】:

    【解决方案2】:

    您可以使用客户 IComparable 类

    public class MyIP : IComparable<MyIP>
    {
        List<int>subAddress = null;
        public MyIP(string IPstr)
        {
           subAddress = IPstr.Split(new char[] {'.'}).Select(x => int.Parse(x)).ToList();
        }
        public int CompareTo(MyIP other)
        {
           int results = 0
           if(this.subAddress[0] != other.subAddress[0])
           {
              results = this.subAddress[0].CompareTo(other.subAddress[0]);
           }
           else
           {
              if(this.subAddress[1] != other.subAddress[1])
              {
                 results = this.subAddress[1].CompareTo(other.subAddress[1]);
              }
              else
              {
                 if(this.subAddress[2] != other.subAddress[2])
                 {
                    results = this.subAddress[2].CompareTo(other.subAddress[2]);
                 }
                 else
                 {
                    results = this.subAddress[3].CompareTo(other.subAddress[3]);
                 }
              }
           }
           return results;
        }
    
    }​
    

    【讨论】:

      猜你喜欢
      • 2018-11-04
      • 2018-03-26
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多