0

I have a ListView which contains data of type

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

Event handler PingCompletedCallback gets IPs in random way, so we can`t predict order of Ips. We need to Sort them. Im using this

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

and it is partially works, but the result looks like this

192.168.1.1 192.168.1.10 192.168.1.2 192.168.1.254 192.168.1.3 and so on...

How can we sort this ListView items in right way

192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.10 192.168.1.254

UPDATE. I tried to do like in that question:

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

but it gives me and exception The type arguments for method Select cannot be inferred from the usage.

Denys Demchenko
  • 218
  • 2
  • 12
  • You need to create a custom sort for the IP address. – rbm Apr 22 '16 at 10:10
  • Possible duplicate of [How to sort list of Ip Addresses using c#](http://stackoverflow.com/questions/6248039/how-to-sort-list-of-ip-addresses-using-c-sharp) – Evan Trimboli Apr 22 '16 at 10:11
  • @Evan Trimboli Sort array of ips is not a problem, i need to sort it and not to loose dependency between IP MAC and HOSTname. So i need to sort all List – Denys Demchenko Apr 22 '16 at 10:13
  • It's exactly the same problem. You need to sort the list of `InfoItem` by ip. Lists are sortable. – Evan Trimboli Apr 22 '16 at 10:17
  • How can i do it: List list = new List(); foreach (var item in lstNetworks.Items) { list.Add(item as InfoItem); } List list2 = new List(); list2 = list.Select(Version.Parse).OrderBy(arg => arg).Select(arg => arg.ToString()).ToList(); gives me and exception The type arguments for method Select cannot be inferred from the usage. – Denys Demchenko Apr 22 '16 at 10:36

2 Answers2

1

As mentioned in the other answer, you can use Version.Parse to do it:

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);
}
Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66
0

You could use a customer IComparable class

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

}​
jdweng
  • 33,250
  • 2
  • 15
  • 20