如何使用C#进行排序IP地址列表如何使用、地址、列表、IP

2023-09-02 01:46:22 作者:Passerby﹌薄情丶

我有IP地址的列表如下:

  192.168.1.5
69.52.220.44
10.152.16.23
192.168.3.10
192.168.1.4
192.168.2.1
 

我在找这样的方式该列表以匹配以下顺序排序

  10.152.16.23
69.52.220.44
192.168.1.4
192.168.1.5
192.168.2.1
 

解决方案 mysql排序IP地址

这看起来是一个黑客,但它不正是你所需要的:

  VAR unsortedIps =
    新[]
    {
        192.168.1.4,
        192.168.1.5,
        192.168.2.1,
        10.152.16.23,
        69.5​​2.220.44
    };

VAR sortedIps = unsortedIps
    。选择(Version.Parse)
    .OrderBy(ARG => ARG)
    。选择(ARG => arg.ToString())
    .ToList();
 

I've a list of IP addresses as follows

192.168.1.5
69.52.220.44
10.152.16.23
192.168.3.10
192.168.1.4
192.168.2.1

I'm looking for such a way to sort this list to match the below order

10.152.16.23
69.52.220.44
192.168.1.4
192.168.1.5
192.168.2.1

解决方案

This might look as a hack, but it does exactly what you need:

var unsortedIps =
    new[]
    {
        "192.168.1.4",
        "192.168.1.5",
        "192.168.2.1",
        "10.152.16.23",
        "69.52.220.44"
    };

var sortedIps = unsortedIps
    .Select(Version.Parse)
    .OrderBy(arg => arg)
    .Select(arg => arg.ToString())
    .ToList();