0

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
  • 1
    I find the example code here for ListViewItemSorter good enough , that you could figure out a solution yourself - adding a hidden column and sorting for that instead of visible IP Address? Commented Mar 25, 2024 at 22:40

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.