Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting List with Custom object by another List using IComparer

My question is about the IComparer interface, I never worked with it before, so I hope you can help me set up everything right.

I have to use the interface to sort an list of own objects by the exact sequence of another List<int>. I couldn't find anything usefull with that problem on the net, everything I found were linq statements, that I can not use.

Here is the example code:

public class What : IComparer<What>
{
    public int ID { get; set; }
    public string Ever { get; set; }

    public What(int x_ID, string x_Ever)
    {
        ID = x_ID;
        Ever = x_Ever;
    }

    public int Compare(What x, What y)
    {
        return x.ID.CompareTo(y.ID);
    }
}

Some data to work with:

List<What> WhatList = new List<What>()
{
    new What(4, "there"),
    new What(7, "are"), 
    new What(2, "doing"),
    new What(12, "you"),
    new What(78, "Hey"),
    new What(63, "?")
};

And the list with the correct order:

List<int> OrderByList = new List<int>() { 78, 4, 63, 7, 12, 2 };

So now how can I tell IComparer to sort by the OrderByList? I really got no clue how to do that, I know this would be pretty easy with linq, but I don't have the opportunity to use it.

like image 908
Tom Pfennig Avatar asked Dec 13 '12 12:12

Tom Pfennig


People also ask

How do I sort lists using IComparable and IComparer?

This article explains how to sort Lists by using two interfaces IComparable<> and IComparer<> provided by the .NET Framework. It's easy to sort a list of strings or integers by just calling the List.Sort () method, but how can we sort two objects and based on what field?

How to sort list of objects in emplist?

Now, calling empList.Sort () gives no exception and empList is well sorted by salary. But sometimes, we may need to sort a list of objects when class does not implement IComparable<> interface and also we may need various kinds of sorting on that class like: Use of IComparer<> interface tells List how exactly you want to sort.

How to sort lists of objects on custom classes in Java?

Sorting Lists is simple as long as you sort basic elements like strings and integers for which comparison classes are defined. Usage of IComparable<> and IComparer<> interface helps to sort Lists of objects on custom classes easily. This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

How to sort a list of objects based on what field?

It's easy to sort a list of strings or integers by just calling the List.Sort () method, but how can we sort two objects and based on what field? Let's look at a small example and see how we can solve the problem using IComparable<> and IComparer interfaces. Let's create a simple Employee class with two fields, Name and Salary.


1 Answers

There are a few tings wrong with your code as it currently stands. If you look at the docs for IComparer<T> you'll see that the T is what you're saying you are going to compare. In your code this is Test but you go on to code for comparisons of What - this means that your code will not compile. See here - error message is:

'Rextester.What' does not implement interface member 'System.Collections.Generic.IComparer.Compare(Rextester.Test, Rextester.Test)'
(Ignore the "Rextester" bit there!).

With all that said and done, you should implement a WhatComparer:

public class WhatComparer : IComparer<What>
{
    private List<int> orderBy;
    public WhatComparer(List<int> orderBy)
    {
        this.orderBy = orderBy;
    }

    public int Compare(What x, What y)
    {
        return orderBy.IndexOf(x.ID).CompareTo(orderBy.IndexOf(y.ID));
    }
}

And use that for ordering:

 WhatList.Sort(new WhatComparer(OrderByList));

Live example: http://rextester.com/BZKO33641

like image 82
Jamiec Avatar answered Nov 02 '22 14:11

Jamiec