C# 按IP对ListView排序
我有一个ListView,其中包含类型为的数据C# 按IP对ListView排序,c#,sorting,C#,Sorting,我有一个ListView,其中包含类型为的数据 class信息项 { 公共字符串IP{get;set;} 公共字符串MAC{get;set;} 公共字符串主机{get;set;} } 事件处理程序PingCompletedCallback以随机方式获取IP,所以我们无法预测IP的顺序。我们需要把它们分类。我在用这个 if (!Dispatcher.CheckAccess()) { Dispatcher.Invoke(new Action(
class信息项
{
公共字符串IP{get;set;}
公共字符串MAC{get;set;}
公共字符串主机{get;set;}
}
事件处理程序PingCompletedCallback以随机方式获取IP,所以我们无法预测IP的顺序。我们需要把它们分类。我在用这个
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 List=新列表();
foreach(lstNetworks.Items中的var项){
列表.添加(作为信息项的项);
}
List list2=新列表();
list2=list.Select(Version.Parse).OrderBy(arg=>arg.Select(arg=>arg.ToString()).ToList();
但它给了我一个例外,方法选择的类型参数不能从用法中推断出来。如另一个答案中所述,您可以使用
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);
}
公共类的东西{
公共字符串ip;
}
var list=新列表(){
新事物(){ip=“192.168.1.1”},
新事物(){ip=“192.168.1.10”},
新事物(){ip=“192.168.1.2”},
新事物(){ip=“192.168.1.254”},
新事物(){ip=“192.168.1.3”}
};
var sorted=list.OrderBy(item=>Version.Parse(item.ip));
foreach(已排序的变量项){
控制台写入线(项目ip);
}
您可以使用customer 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;
}
}
公共类MyIP:i可比较
{
ListsubAddress=null;
公共MyIP(字符串IPstr)
{
subAddress=IPstr.Split(新字符[]{.'.})。选择(x=>int.Parse(x)).ToList();
}
公共int比较(MyIP其他)
{
int结果=0
if(this.subAddress[0]!=other.subAddress[0])
{
结果=此.subAddress[0]。比较(其他.subAddress[0]);
}
其他的
{
if(this.subAddress[1]!=other.subAddress[1])
{
结果=this.subAddress[1]。CompareTo(其他.subAddress[1]);
}
其他的
{
if(this.subAddress[2]!=other.subAddress[2])
{
结果=this.subAddress[2]。CompareTo(其他.subAddress[2]);
}
其他的
{
结果=this.subAddress[3]。CompareTo(其他.subAddress[3]);
}
}
}
返回结果;
}
}
您需要为IP地址创建自定义排序。IP的@Evan Trimboli排序数组的可能重复不是问题,我需要对其进行排序,并且不要失去IP MAC和主机名之间的依赖关系。所以我需要对所有列表进行排序,这是完全相同的问题。您需要按ip对InfoItem
列表进行排序。列表是可排序的。如何进行排序:List List=new List();foreach(lstNetworks.Items中的var项){list.Add(项作为InfoItem);}list list2=new list();list2=list.Select(Version.Parse).OrderBy(arg=>arg.Select(arg=>arg.ToString()).ToList();为我和异常提供了无法从用法推断出的方法Select的类型参数。
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;
}
}