C# IComparer multi sorting w delegates | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

C# IComparer multi sorting w delegates

I made a new attempt to do this sorting. This time with delegates to carry information about search criteria: I try to sort a list of objects using icompare. I Want to be able to sort the objects with different criterias. How do I pass the criteria to the sorting class? Here, I pass the sorting method to the constructor. Is there a better Way? https://code.sololearn.com/c8N9pcJ1iG0v/?ref=app I have been adviced to use Linq instead. But the purpose is to examine IComparer possibilities

17th Feb 2022, 7:14 PM
Joakim Eriksson
Joakim Eriksson - avatar
6 Answers
+ 5
In the following code I use the Comparison<T> delegate with List.Sort() method, to achieve a particular ordering. https://docs.microsoft.com/en-us/dotnet/api/system.comparison-1?view=net-6.0 In my understanding, - Comparer abstract class and IComparer interface, are in the toolset of object-oriented programming - Comparison delegate represents a functional programming style I much more prefer the latter 🙂 It is my firm belief, that most of the "best practice" design patterns, used in enterprise development, are a sad consequence of the flaws and limitations of the OOP paradigm, that applies to most mainstream languages. They introduce unnecessary complexity that divert the attention of the programmer from the REAL problems and from the actual business domains. https://code.sololearn.com/cMfs9sa6UiGw/?ref=app
18th Feb 2022, 4:45 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Some sources to check: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.icomparer-1?view=net-6.0 https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.comparer-1?view=net-6.0 "We recommend that you derive from the Comparer<T> class instead of implementing the IComparer<T> interface, because the Comparer<T> class provides an explicit interface implementation of the IComparer.Compare method and the Default property that gets the default comparer for the object."
17th Feb 2022, 8:02 PM
Tibor Santa
Tibor Santa - avatar
+ 3
You should probably seperate your code into two IComparer/Comparer classes. class NameComparer : Comparer<Animal> { ... } class AgeComparer : Comparer<Animal> { ... } Because the two implementations don't really need to know about each other. Then you can just create the correct instance of whichever comparer you need. And if you want, you could add a factory method, something like.. static class AnimalComparerFactory { public static Comparer<Animal> Create (string comparatorType) => comparatorType switch { "name" => new NameComparer(), "age" => new AgeComparer(), _ => throw new ArgumentException("You picked the wrong name dummy") } } And later of course: var comparer = AnimalComparerFactory.Create("name"); This is what I would consider the "cleanest" approach. (Single responsibility: each class should only do one thing)
17th Feb 2022, 8:51 PM
Schindlabua
Schindlabua - avatar
+ 3
Tibor Santa Nice, I didn't know about that! Comparer is certainly very OO and the signal to noise ratio is very low. I'll never write another Comparer again, this is a lot better :P
18th Feb 2022, 7:34 AM
Schindlabua
Schindlabua - avatar
+ 2
You are absolutely right! There should be separate classes for each type of sort!🤔(I actually started out with separate classes but I did not know how to ”call” the right class from my listmanagers sortfunction). I like the comparerfactory🙂. Thank you! I will play around with this a bit… 🙏
17th Feb 2022, 10:06 PM
Joakim Eriksson
Joakim Eriksson - avatar
+ 2
My original question was a bit open, hoping to get this kind of answeres. Not just targeting the specific issue but also adressing the bigger picture! I thought functional programming was an abandoned way of programming. On the other hand in todays agile world… This was my first thought when I looked at my zoo sorting problem. Why do this feel so complicated. Now I have seen so many nice alternatives! 🌴😄
18th Feb 2022, 12:09 PM
Joakim Eriksson
Joakim Eriksson - avatar