How to Sort Records in a ListView

45 Views Asked by At

I would like to use the following class:

internal class IPComparer : IComparer<string>
{

    public int Compare(string a, string b)
    {
        return Enumerable.Zip(a.Split('.'), b.Split('.'),
            (x, y) => int.Parse(x).CompareTo(int.Parse(y))).FirstOrDefault(i => i != 0);
    }
}

to sort IP addresses in a C# ListView by redefining the ListViewItemSorter method. enter image description here

Could you tell me how to do it? Thanks

1

There are 1 best solutions below

0
Pepe Alvarez On

In .NET there exists a class in which you use a comparer so you can have a queue sorted upon insertions: PriorityQueue<TElement,TPriority>(IComparer) Class. This can be a good alternative to what you wanted to try. Which can be really efficient if you are going to perform multiple insertions and then extracting values, instead of sorting every single time you add a new value.

Represents a collection of items that have a value and a priority. On dequeue, the item with the lowest priority value is removed. Implements an array-backed, quaternary min-heap. Each element is enqueued with an associated priority that determines the dequeue order. Elements with the lowest priority are dequeued first. Note that the type does not guarantee first-in-first-out semantics for elements of equal priority.