krisgithub1234 / linqbridge

Automatically exported from code.google.com/p/linqbridge
Other
0 stars 0 forks source link

Nondeterministic keySelector in OrderBy #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

There is a difference in the implementation of OrderedEnumerable, which affects 
the way how keySelector is threated.

Consider the following example:

        [Test]
        public void OrderByNonDeterministic()
        {
            var values = "abcd".ToCharArray();
            int key = values.Count();
            values = values.OrderBy(_ => key--).ToArray();

            Assert.AreEqual("dcba", new String(values));
            Assert.AreEqual(0, key, "Key for every element is computed only once");

        }

keySelector is evaluated only once for each element in System.Core, and then 
resulting key array is sorted.
As an opposite in LinqBridge keySelector is computed for every two elements 
being compared. So it produces different result. Even for deterministic 
keySelector this can hurt performance.

According to the documentation of OrderBy there are no limitations to the 
keySelector function, except that Comparer<TKey>.Default must exist for key 
values being returned, if such comparer isn't provided for OrderBy.

Original issue reported on code.google.com by mrdont@mail.ru on 31 May 2011 at 9:49

GoogleCodeExporter commented 9 years ago

Original comment by azizatif on 16 Apr 2012 at 11:08

GoogleCodeExporter commented 9 years ago
This issue was closed by revision b09ed8a618f6.

Original comment by azizatif on 16 Apr 2012 at 5:07