How does LINQ sort positions by given criteria?

Suppose we have a class MyData that implements the IComparable<MyData> and IComparable . Then we have a list containing many MyData elements and a LINQ query to get a sorted list.

 public class MyData : IComparable<MyData>, IComparable { ... public int CompareTo(MyData value) { // TODO } public int CompareTo(object value) { if (value == null) return 1; if (value.GetType() == typeof(MyData)) { MyData rightValue = (MyData)value; return (this.CompareTo(rightValue)); } else throw new ArgumentException("Object is not a " + typeof(MyData) + "."); } } // main method List<MyData> list = new List<MyData>(); ... var items = from item in list orderby item descending select item; 

When LINQ sorts items in a list , does it use an implementation of the IComparable interface in the MyData class?

If so, is it better to encapsulate the sorting criteria in the MyData class (by implementing the above interfaces) or specify the criteria in the LINQ query (without MyData , which implements these interfaces)? What are the pros and cons of these two options?

+4
source share
3 answers

Enumerable.OrderBy "compares keys using the default comparison. Default ", which, in turn, will use your implementation of IComparable<T> .

If so, is it better to encapsulate the sorting criteria in the MyData class (by implementing the above interfaces) or specify the criteria in the LINQ query (without MyData, which implements these interfaces)? What are the pros and cons of these two options?

So first, yes.

Both approaches have advantages.

The implementation of IComparable<T> suggests that the type has a natural order. When it's true, I like to implement this interface. The main professional, from a LINQ point of view, is that you simplify your LINQ queries (a bit). However, my main professional is more likely to offer a โ€œnaturalโ€ order for this type, which in turn adds clarity to your API.

The primary profile for defining criteria in the LINQ query itself is flexibility. This allows you to sort by any number of criteria - without limiting yourself to a specific type, defined in the type itself. If a type does not have a โ€œnaturalโ€ sort order (i.e., it does not represent something that is a โ€œquantityโ€, everything is at its discretion or similar), I would personally use this method.

+5
source

In extension methods, OrderBy accepts the delegate Func<> - this is what is used to select the item used for sorting. One of the overloads takes an instance of IComparer for comparison - those that do not use the Default mapping.

Take a look at EduLinq - Jon Skeet's LINQ educational reintegration to see how it's done - here's the OrderBy post .

+3
source

In this case, it uses Enumerable.OrderBy , which uses Default Comparer:

This method compares keys using the default default comparison.

This method performs stable sorting; that is, if the keys of the two elements are equal, the order of the elements is preserved. In contrast, an unstable view does not preserve the order of elements that have the same key.

It is probably important to note that you used Linq2SQL or some other LINQ provider; this is not necessarily the case. This is true only because it is an implementation of IEnumerable (Linq2Objects).

+2
source

Source: https://habr.com/ru/post/1396488/


All Articles