Suspicious sortings in Unity, ASP.NET Core, and more
Some believe that experienced developers do not make silly errors. Comparison errors? Dereferencing null references? Bet you think: “No, it’s definitely not about me…” ;) By the way, what about errors with sorting? As the title suggests, there are some nuances.
Let me give you an example to describe the problem. Let’s say we have some type (Wrapper) with two integer properties (Primary and Secondary). There’s an array of instances of this type. We need to sort it in ascending order. First — by the primary key, then — by the secondary key.
Here’s the code:
Unfortunately, the result of this code will be incorrect:
The sequence turned out to be sorted by the secondary key. But the sorting by primary key was not saved. If you’ve ever used multilevel sorting in C#, you can guess what the catch is.
The second OrderBy method call introduces a new primary ordering. This means that all the sequence will be sorted again.
But we need to fix the result of primary sorting. The secondary sorting should not reset it.
In this case the correct sequence of calls is OrderBy(…).ThenBy(…):
Then the code produces the expected result:
Microsoft has the documentation for the ThenBy method. There’s a note about this: Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new primary ordering that ignores the previously established ordering.
Want to know what I found? ;)
Examples from open-source projects
In Unity, the analyzer found 2 similar code fragments.
The first fragment
Perhaps, the developers wanted to sort the m_Children collection first by key (c.key), then by priority (c.priority). But sorting by priority will be performed on the entire collection. Sorting by key will not be fixed. Is this an error? Here we need to ask the developers.
The second fragment
he sorting results in the following order:
- the sequence starts with the elements with providers. The elements without providers follow them. We can say that we have 2 “groups”: with providers and without them;
- in these groups the elements are sorted by priority.
Perhaps, there is no error here. However, agree that the sequence of the OrderBy().ThenBy() calls is easier to read.
I reported both problems via Unity Bug Reporter. After this, Unity QA Team opened 2 issues:
Issues don’t contain any comments yet. So, we are still waiting for any updates.
PVS-Studio found 3 places in ASP.NET Core with duplicated OrderBy calls. All were detected in the KnownHeaders.cs file.
The first issue
The second issue
The third issue
The error pattern is the same, only the used variables are different. To report these issues, I created a new issue on the project page.
Developers answered that duplicated OrderBy calls aren’t bugs. Nevertheless, they’ve fixed the code. You can find a commit here.
In any case, I think that you should not write code in such a way. Duplicated OrderBy calls look very suspicious.
Here we’re dealing with a strange sorting by the fields of the int? type. I also created an issue for this. In this case, the secondary sorting turned out to be redundant. That’s why the developers deleted the OrderByDescending call. You can find the commit here.
Perhaps, the developers wanted to perform sorting first by version, then — by priority. As a result, sorting is performed only by priority.
As with the previous issues, I informed the developers. They fixed this by replacing the second OrderBy call with ThenBy:
You can find the fix here.
The sequence of OrderBy().OrderBy() calls may not be an error. But such code provokes questions. Is it correct? What if OrderBy().ThenBy() should be used here?
How can developers make such errors?
Perhaps, it is a human reliability. We know that developers tend to make errors in comparison functions. Also, there’s the last line effect. Moreover, copy-paste often provokes errors. Perhaps the multiple OrderBy call is another manifestation of human reliability.
Anyway, be careful with this. :)
Following a good tradition, I invite you to follow me on Twitter so as not to miss interesting publications.
Finally, please tell me: have you encountered a similar pattern?