GroupBy of List<double> with tolerance doesn't work
<p>I have a question about <code>Groupby</code> of C#.</p>

<p>I made a <code>List</code> like shown below:</p>

<pre><code>List&lt;double&gt; testList = new List&lt;double&gt;();

testList.Add(1);    
testList.Add(2.1);  
testList.Add(2.0);  
testList.Add(3.0);  
testList.Add(3.1);  
testList.Add(3.2);  
testList.Add(4.2);  
</code></pre>

<p>I'd like to group these number list like this:</p>

<pre><code>group 1 =&gt; 1  
group 2 =&gt; 2.1 , 2.0  
group 3 =&gt; 3.0 , 3.1 , 3.2  
group 4 =&gt; 4.2
</code></pre>

<p>so, I wrote code like this:</p>

<pre><code>var testListGroup = testList.GroupBy(ele =&gt; ele, new DoubleEqualityComparer(0.5));
</code></pre>

<p><code>DoubleEqualityComparer</code> definition is like this:</p>

<pre><code>public class DoubleEqualityComparer : IEqualityComparer&lt;double&gt;
{
    private double tol = 0;

    public DoubleEqualityComparer(double Tol)
    {
        tol = Tol;
    }

    public bool Equals(double d1,double d2)
    {
        return EQ(d1,d2, tol);
    }

    public int GetHashCode(double d)
    {
        return d.GetHashCode();
    }
    public bool EQ(double dbl, double compareDbl, double tolerance)
    {
        return Math.Abs(dbl - compareDbl) &lt; tolerance;
    }
}
</code></pre>

<p>Yet the <code>GroupBy</code> clause doesn't work like the this:</p>

<pre><code>group 1 =&gt; 1  
group 2 =&gt; 2.1
group 3 =&gt; 2.0  
group 4 =&gt; 3.0
group 5 =&gt; 3.1
group 6 =&gt; 3.2
group 7 =&gt; 4.2
</code></pre>

<p>I don't know what the problem is. Please let me know if there is problem, and solutions.</p>