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();
This will give you "10.152.16.23","192.168.1.4", "192.168.1.5", "192.168.2.1", "69.52.220.44"
@Norbert - please try the code before stating that. I actually checked the result before posting.
@Alex nice solution, thnx Man
@Alex that's actually a brilliant idea, thanks, it saved me time
I had a
List<IPAddress>
and sorted it like this:lstIp = lstIp.OrderBy(i => new Version(i.ToString())).ToList();