Hi.
I'm wondering if there's a easy way to set a custom order function to a grid column. For example, suppose that I need to sort a column with IP Addresses:
192.168.168.1
192.168.168.2
192.168.168.101
192.168.168.4
192.168.168.20
Usually the grid sort this column like a string, so the result is:
192.168.168.1
192.168.168.101
192.168.168.2
192.168.168.20
192.168.168.4
when the desirable result will be:
192.168.168.1
192.168.168.2
192.168.168.4
192.168.168.20
192.168.168.101
I did several attempts without success. Order by a calculated field created in the column template looks like a workaround more than a real solution. Really don't like it.
Reading about the GridState I've found there is a property: SortCompare (part of SortDescriptor) but I can't make it work assigning a function like this:
public class IPAddressComparer : IComparer<IpAddress>
{
public int Compare(IpAddress a, IpAddress b)
{
return Enumerable.Zip(
a.Address.Split('.'),
b.Address.Split('.'),
(x, y) => int.Parse(x).CompareTo(int.Parse(y)))
.FirstOrDefault(i => i != 0);
}
}
What else can I try? Maybe I'm missing something... can't be so complicated.
Blazorist.