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.
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?
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.
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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With